Javascript:将多个参数作为单个变量传递

发布于 2024-09-14 06:46:28 字数 323 浏览 4 评论 0原文

是否可以使用单个变量传递多个参数?例如,如果我想做类似的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

妳是的陽光 2024-09-21 06:46:28
function foo(thing) {
    document.write("X is " + thing.x);
    document.write("Y is " + thing.y);
}

var bar = {x:0, y:10};
foo(bar);
function foo(thing) {
    document.write("X is " + thing.x);
    document.write("Y is " + thing.y);
}

var bar = {x:0, y:10};
foo(bar);
千笙结 2024-09-21 06:46:28

你所要求的是不可能的。如果您想在单个参数中传递多个值,请使用ArrayObject。如果您确实必须使用字符串,则必须调用split()将参数字符串分解为数组。

What you're asking for is impossible. If you want to pass multiple values in a single argument, use an Array or an Object. If you really must use a string, you'll have to call split() to break the argument string into an array.

爱殇璃 2024-09-21 06:46:28
function Add (a, b, c) {
    return a + b + c;
}

var nums = [1, 2, 4];
var sum = Add.apply (null, nums);

可变长度参数列表:

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);

参考:apply 方法(Function 对象)

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)

我纯我任性 2024-09-21 06:46:28

当然,传递选项对象是很常见的

function foo(options){
  //...
}

,然后您可以传递任何内容...

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

通常这些是内联定义的,特别是如果在方法调用之外不需要这些值的话。

foo({'x':5,'y':23,'border':3});

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});
说好的呢 2024-09-21 06:46:28

并不真地。

你可以这样做:

window.foo.apply(window, bar.split(','));

(Apply 允许你传递一个参数数组,而不是分别传递每个参数)

……但是我想到了“丑陋”这个短语。

Not really.

You could do:

window.foo.apply(window, bar.split(','));

(Apply lets you pass an array of arguments instead of each argument separately)

… but the phrase "ugly" comes to mind.

苏佲洛 2024-09-21 06:46:28

你可以使用这个:

var bar = [0,10]; // creates an array
foo(bar);

function foo(arg){
    document.write("X is " + arg[0]);
    document.write("Y is " + arg[1]);
}

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]);
}
相思碎 2024-09-21 06:46:28

不,但您可以传递数组或对象:

function foo(options){
    document.write("X is " + options.x);
    document.write("Y is " + options.y);
}

var bar = {x: 0, y:10};

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};
始于初秋 2024-09-21 06:46:28

不,这是不可能的。您可以将两个参数放入一个数组中,但数组仍然是一个变量。然后,您需要重写该函数以接受一个变量,并将其视为一个数组,如下所示:

function foo(x){
document.write("X is " + x[0]);
document.write("Y is " + x[1]);
}

基本上,函数接受变量作为参数,并且无论您传递哪种变量,每个变量仍然只是一个变量- 无法将单个变量识别为多个参数。数组是一个变量,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:

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.

一笑百媚生 2024-09-21 06:46:28

怎么样? (对于 ES6+)

function foo({x, y}){
    document.write("X is " + x);
    document.write("Y is " + y);
}

并调用它:

foo({x:10, y:5})

在多个参数上使用单个结构化参数有一个缺点,即对于多个参数,您可以在许多 IDE 中使用 /** 来生成方法头它将为每个参数显示一个@param
但是,如果您只有一个参数,那么您将失去每个参数的描述的准确性,因此 IDE 中的智能感知不太有用,因为它不会获取结构属性的文档。

/**
 * Do stuff
 * @param {*} param0 - A structure containing the blah, blah blah data
 */
function foo({x, y}){

而不是..

/**
 * 
 * @param {*} x - The value for blah
 * @param {*} y - the value for blah-blah
 */
foo1(x, y){

How about? (For ES6+)

function foo({x, y}){
    document.write("X is " + x);
    document.write("Y is " + y);
}

and call it with:

foo({x:10, y:5})

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.

/**
 * Do stuff
 * @param {*} param0 - A structure containing the blah, blah blah data
 */
function foo({x, y}){

instead of..

/**
 * 
 * @param {*} x - The value for blah
 * @param {*} y - the value for blah-blah
 */
foo1(x, y){
兮颜 2024-09-21 06:46:28

直接回答你的问题,没有。值得注意的是,您定义 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".

残龙傲雪 2024-09-21 06:46:28
function myFunction(a,b){
//do stuff with a and b here
}

myFunction(1,'text')

或者...

<a onClick="myFunction(1,'text');"

此处有一篇关于该问题的文章。

function myFunction(a,b){
//do stuff with a and b here
}

myFunction(1,'text')

or...

<a onClick="myFunction(1,'text');"

There's an article on the issue here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文