简体   繁体   中英

Define Javascript Argument Names With An Array

I'm looking for a way to define javascript function arguments by array so that, for example, the following code would work. Is it possible?

args = ["foo", "bar"]
func = function(???) { //not sure what (if anything) could go here to make it work
    console.log(foo)
    console.log(bar)
}
func (5, "good times") // logs "5" and "good times"

EDIT: The offered solutions are good but don't address the problem because retention of the specific name is important. I have a function with an extremely long list of arguments, and I'd rather define them as an array then in the actual function definition.

Inside a function, the arguments are available via a special object called arguments :

function magic() {
  for (var i = 0; i < arguments.length; ++i)
    console.log(arguments[i]);
}

magic("hello", "world"); // two console entries, "hello" and "world"

The arguments object is like an array, but it's different. It's important to be careful using it if performance is important, because the object has some unusual properties that cause problems for modern JavaScript optimizers.

It is not possible to give names to parameters after the function call is made, though (if you really wanted to) you could create an object and copy the arguments into it:

function weird() {
  var parameters = {};
  for (var i = 0; i < arguments.length; ++i)
    parameters["arg" + i] = arguments[i];
  // ...
}

You could just pass an array and using a for to loop through array's values:

func = function(array) {
    for(var i=0; i<array.length; i++){
        console.log(array[i]);
    }
}

Perhaps you are looking for the .apply() method which allows you to pass a series of arguments as an array and it will unpack them into normal arguments when the function is called.

var args = ["foo", "bar"];
var func = function() {
    // any arguments pass to the function can be accessed via
    // the arguments pseudo-array
    console.log(arguments[0]);   // "foo"
    console.log(arguments[1]);   // "bar"
}
func.apply(null, args);

Or, you could still declare names for the arguments in the function declaration:

var args = ["foo", "bar"];
var func = function(arg1, arg2) {
    console.log(arg1);    // "foo"
    console.log(arg2);    // "bar"
}
func.apply(null, args);

If you want named arguments where the caller sets the names that are passed, you can pass an object to the function:

var args = {foo: "goodbye", bar: "hello"};
var func = function(options) {
    console.log(options.foo);    // "goodbye"
    console.log(options.bar);    // "hello"
}
func(args);

It looks like you're looking for keyword arguments, like in python:

 # define a func    
 def func(foo, bar):
   ....

 # call it
 func(bar=123, foo=456)

Javascript (as of ES5) doesn't provide this kind of syntax, it's usually simulated using objects:

 function func(args) {
    console.log(args.foo);
    console.log(args.bar);
 }

 // call it
 func({
    bar: 123,
    foo: 456
 });

Inspired by @pointy's answer, I came up with a way to do this, though it does pollute the global namespace a bit .

args = ["foo", "bar"]
func = function() {
    for (i = 0, len = args.length; i < len; i = ++i) {
        arg = args[i];
        window[arg] = arguments[i];
    }
    console.log(foo)
    console.log(bar)
}
func (5, "good times") // logs "5" and "good times"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM