Javascript:将多个参数作为单个变量传递
是否可以使用单个变量传递多个参数?例如,如果我想做类似的事情:
function foo(x,y){
document.write("X is " + x);
document.write("Y is " + y);
}
var bar = "0,10";
foo(bar);
上面的示例是我尝试做的事情的简化示例。它不起作用(因为“bar”被检测为单个参数)。我知道有更简单的方法可以使用数组来实现这一点。
所以,我问这个问题主要是出于好奇——是否有可能将“bar”变量检测为不是一个参数,而是两个参数?
谢谢!
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.
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?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
你所要求的是不可能的。如果您想在单个参数中传递多个值,请使用
Array
或Object
。如果您确实必须使用字符串,则必须调用split()
将参数字符串分解为数组。What you're asking for is impossible. If you want to pass multiple values in a single argument, use an
Array
or anObject
. If you really must use a string, you'll have to callsplit()
to break the argument string into an array.可变长度参数列表:
参考:apply 方法(Function 对象)
variable-length argument list:
Reference: apply method (Function object)
当然,传递选项对象是很常见的
,然后您可以传递任何内容...
通常这些是内联定义的,特别是如果在方法调用之外不需要这些值的话。
Sure, this is common to pass an object for options
then you can pass in anything...
Often these are defined inline, especially if the values are not needed outside of the method call.
并不真地。
你可以这样做:
(Apply 允许你传递一个参数数组,而不是分别传递每个参数)
……但是我想到了“丑陋”这个短语。
Not really.
You could do:
(Apply lets you pass an array of arguments instead of each argument separately)
… but the phrase "ugly" comes to mind.
你可以使用这个:
You may use this:
不,但您可以传递数组或对象:
No, but you could pass a an array or object:
不,这是不可能的。您可以将两个参数放入一个数组中,但数组仍然是一个变量。然后,您需要重写该函数以接受一个变量,并将其视为一个数组,如下所示:
基本上,函数接受变量作为参数,并且无论您传递哪种变量,每个变量仍然只是一个变量- 无法将单个变量识别为多个参数。数组是一个变量,JSON 对象是一个变量,等等。这些东西有多个部分,但它们被单个变量封装。
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:
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.
怎么样? (对于 ES6+)
并调用它:
在多个参数上使用单个结构化参数有一个缺点,即对于多个参数,您可以在许多 IDE 中使用
/**
来生成方法头它将为每个参数显示一个@param
。但是,如果您只有一个参数,那么您将失去每个参数的描述的准确性,因此 IDE 中的智能感知不太有用,因为它不会获取结构属性的文档。
而不是..
How about? (For ES6+)
and call it with:
There is a downside to using a single structured argument over multiple arguments, and that is with multiple arguments you can use
/**
in may IDEs to generate a method header which will display an@param
for each argument.But if you only have one argument then you will lose the niceness of a description for each argument and hence less useful intelli-sense in the IDE as it wont pick up the docuemntation of the structure's properties.
instead of..
直接回答你的问题,没有。值得注意的是,您定义 bar 的方式只是一个值,即包含“0,10”的字符串。
To directly answer your question, no. It's worth noting that the way you have
bar
defined it's only one value, a string containing "0,10".或者...
此处有一篇关于该问题的文章。
or...
There's an article on the issue here.