1

I would like to create Javascript object from Javascript String.

var arrayOfName = new Array();
arrayOfName.push("module1");
arrayOfName.push("module2");
arrayOfName.push("module3");
arrayOfName.push("module4");

For this example, i would like to create 4 objects : module1, module2, module3 and module4.

And after, how can i passed these object as a parameters of a function ? like this :

this.myFunctionTest = function myFunctionTest(module1, module2, module3, module4) {
  // ...
}

Ideally, the code must work for all elements which are in the Javascript Array called "arrayOfName".

2 Answers 2

1

Using .apply function of Function prototype

myFunctionTest.apply(this, arrayOfName);

Note: You can find more here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

Sign up to request clarification or add additional context in comments.

2 Comments

Is this will work if i want to pass as parameter of require ? Like require(arrayOfPath, function(arrayOfName) {})
Can you explain it more ? Answer is yes, but I'm confused with names of variables and from what framewrok it is
1

You can use Function.prototype.apply like this

myFunctionTest.apply(this, arrayOfName);

The apply function passes all the elements of arrayOfName as parameters to myFunctionTest.

As the number of parameters grow in number, you may not want to or find it difficult to maintain the number or parameters. Don't worry, JavaScript has got that covered :) It has something called arguments which is an array like object. You can access the first parameter with arguments[0], like accessing a normal array object.

var arrayOfName = [];
arrayOfName.push("module1");
arrayOfName.push("module2");
arrayOfName.push("module3");
arrayOfName.push("module4");

function myFunctionTest(param1, param2, param3) {
    console.log(param1, param2, param3);
    console.log(arguments);
}

myFunctionTest.apply(this, arrayOfName);
// module1 module2 module3
// { '0': 'module1', '1': 'module2', '2': 'module3', '3': 'module4' }

If you want to require node modules, with this method

var modules = ["lodash", "underscore", "q"];
require.apply(this, modules);

But if you want to get the objects returned by the require for each of the modules, then your best bet would be using Array.prototype.map with require, like this

console.log(modules.map(require));

If you have a function like this

function modules(module1, module2, ...) {
    ...
}

And if you want to invoke it with all the loaded modules, then you would do something like this

modules.apply(this, modules.map(require));

3 Comments

Is this will work if i want to pass as parameter of require ? Like require(arrayOfPath, function(arrayOfName) {})
@wawanopoulos You mean, you want to require multiple modules at once?
To be more compleete, i have some string in entry representing module name. After, i must do an internal search to find the correct path of each module, and after genereate some object to pass in function parameter of require. At the end, i would like to have something like this : require(["../path/module1", "../path/module2"], function(module1, module2) { .....}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.