Javascript:将多个参数作为单个变量传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3471337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: passing multiple arguments as a single variable
提问by user418797
is it possible to pass multiple arguments using a single variable? For example, if I wanted to do something like:
是否可以使用单个变量传递多个参数?例如,如果我想做类似的事情:
function foo(x,y){
document.write("X is " + x);
document.write("Y is " + y);
}
var bar = "0,10";
foo(bar);
The example above is an simplified example of what I was trying to do. It doesn't work (because the "bar" is detected as a single argument). I know that there are easier ways to implement this using arrays.
上面的例子是我试图做的事情的一个简化例子。它不起作用(因为“bar”被检测为单个参数)。我知道有更简单的方法可以使用数组来实现这一点。
So, I ask this question mostly out of curiosity - is it possible to get the "bar" variable to be detected as not one, but 2 arguments?
所以,我问这个问题主要是出于好奇 - 是否有可能将“bar”变量检测为不是一个,而是 2 个参数?
Thanks!
谢谢!
回答by jtbandes
function foo(thing) {
document.write("X is " + thing.x);
document.write("Y is " + thing.y);
}
var bar = {x:0, y:10};
foo(bar);
回答by JSB????
What you're asking for is impossible. If you want to pass multiple values in a single argument, use an Arrayor an Object. If you really mustuse a string, you'll have to call split()to break the argument string into an array.
你所要求的是不可能的。如果要在单个参数中传递多个值,请使用 anArray或 an Object。如果您确实必须使用字符串,则必须调用split()将参数字符串分解为数组。
回答by gumape
function Add (a, b, c) {
return a + b + c;
}
var nums = [1, 2, 4];
var sum = Add.apply (null, nums);
variable-length argument list:
变长参数列表:
function Add () {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
var n = Add (1, 2, 3, 4, 5);
Reference: apply method (Function object)
回答by Quentin
Not really.
并不真地。
You could do:
你可以这样做:
window.foo.apply(window, bar.split(','));
(Apply lets you pass an array of arguments instead of each argument separately)
(Apply 允许您传递参数数组而不是单独传递每个参数)
… but the phrase "ugly" comes to mind.
……但我想到了“丑陋”这个词。
回答by scunliffe
Sure, this is common to pass an object for options
当然,这是常见的传递一个对象的选项
function foo(options){
//...
}
then you can pass in anything...
然后你可以传入任何东西......
var opts = {};//create an object
opts['x'] = 5;//set whatever properties you want
opts['y'] = 23;
opts['border'] = 3;
foo(opts);//pass 1 argument, with as many values as you want
Often these are defined inline, especially if the values are not needed outside of the method call.
通常这些是内联定义的,特别是如果在方法调用之外不需要这些值。
foo({'x':5,'y':23,'border':3});
回答by Alex Zylman
No, it's not possible. You could put two arguments in an array, but an array is still one variable. Then you would need to rewrite the function to accept one variable, and treat it as an array, like this:
不,这不可能。您可以将两个参数放入一个数组中,但数组仍然是一个变量。然后您需要重写该函数以接受一个变量,并将其视为一个数组,如下所示:
function foo(x){
document.write("X is " + x[0]);
document.write("Y is " + x[1]);
}
Basically, a function accepts variables as arguments and, no matter what kind of variable you pass it, each variable is still only one variable - there's no way to get a single variable to be recognized as multiple arguments. An array is one variable, a JSON object is one variable, etc. These things have multiple parts to them, but they're encapsulated by a single variable.
基本上,一个函数接受变量作为参数,无论你传递什么样的变量,每个变量仍然只是一个变量——没有办法让单个变量被识别为多个参数。数组是一个变量,JSON 对象是一个变量,等等。这些东西有多个部分,但它们被一个变量封装。
回答by prodigitalson
No, but you could pass a an array or object:
不,但您可以传递一个数组或对象:
function foo(options){
document.write("X is " + options.x);
document.write("Y is " + options.y);
}
var bar = {x: 0, y:10};
回答by Topera
You may use this:
你可以使用这个:
var bar = [0,10]; // creates an array
foo(bar);
function foo(arg){
document.write("X is " + arg[0]);
document.write("Y is " + arg[1]);
}
回答by Robert
To directly answer your question, no. It's worth noting that the way you have bardefined it's only one value, a string containing "0,10".
直接回答你的问题,不。值得注意的是,您bar定义它的方式只有一个值,一个包含“0,10”的字符串。

