简体   繁体   English

如何在JavaScript函数中使用可变数量的参数

[英]How to use a variable number of arguments in a JavaScript function

I am having trouble understanding how to return information to the first function from the second when there are multiple arguments. 当有多个参数时,我很难理解如何将信息从第二个函数返回到第一个函数。 Now I know the following code works. 现在,我知道以下代码有效。

function One() {
    var newVal = 0;
    newVal = Too(newVal);
    console.log(newVal);
}

function Too(arg) {
    ++arg;
    return arg;
}

But what if I try to complicate things by adding arguments and a setinterval. 但是,如果我尝试通过添加参数和setinterval使事情复杂化怎么办?

function One() {
    var newVal = 0;
    var z = 3;
    var y = 3;
    var x = 1;
    newVal = Too(newVal);
    var StopAI2 = setInterval(function () {
        Too(x, y, z, newVal)
    }, 100);
}

function Too(Xarg, Yarg, Zarg, newValarg) {
    Xarg*Xarg;
    Yarg*Yarg;
    Zarg*Zarg;
    ++newValarg;
    return newValarg;
}

I'm not sure what to do with the newVal = line of code. 我不确定如何处理newVal =代码行。 I only want to return the newVal not x,y,z. 我只想返回newVal而不是x,y,z。

This is what I think you're trying to ask: 我想这就是您要问的问题:

How can I operate on the 4th argument to a function when only one argument is passed? 仅传递一个参数时,如何对函数的第四个参数进行操作?

The answer to that question is this: 这个问题的答案是这样的:

If you want to operate on the 4th argument of a function, at least 4 arguments must be passed to the function. 如果要对函数的第4个参数进行操作,则必须至少将4个参数传递给该函数。

There are a few ways you can approach your problem differently. 您可以通过几种方法来不同地解决问题。

#1 #1

If there's one argument that is always necessary, make sure it's the first argument: 如果总是有一个参数,请确保它是第一个参数:

 function Too(mandatoryArg, optionalArg1, optionalArg2) {
    alert(++mandatoryArg);
    if (optionalArg1) {
        alert(++optionalArg1);
    }
}

#2 #2

Pass placeholder values for all the undefined or unknown arguments. 为所有未定义或未知参数传递占位符值。

You might use null , undefined , or '' . 您可以使用nullundefined''

alert(Too(null, null, 4));

function Too(optArg1, optArg2, mandatoryArg) {
    alert(++mandatoryArg);
}

#3 #3

Make a decision based on the number of arguments: 根据参数数量做出决定:

function Too(optArg1, optArg2, optArg3) {
    var numArgs = arguments.length;
    if (numArgs === 1) {
        alert(++optArg1);
    }
    if (numArgs === 3) {
        alert(++optArg3);
    }
}


EDIT 编辑

"Will this update a variable in the first function?" “这会更新第一个函数中的变量吗?”

Let's use an actual example that demonstrates something: 让我们用一个实际的例子演示一下:

function one() {
    var a = 0;
    var b = 25;
    var c = 50;
    var d = -1;

    d = two(a, b, c);

    alert("a: " + a);
    alert("b: " + b);
    alert("c: " + c);
    alert("d: " + d);
}

function two(a, b, c) {
    ++a;
    ++b;
    ++c;
    if (arguments.length === 1) {
        return a;
    }
    if (arguments.length === 3) {
        return c;
    }
}

Invoking one() will cause the following alerts: 调用one()将导致以下警报:

a: 0
b: 25
c: 50
d: 51

Only the value of d is modified in function one() . 在函数one()中仅修改d的值。

That's because d is assigned the return value of two() . 这是因为d被分配了two()的返回值。

The changes to a , b , and c , inside two() have no effect on the values of a , b , and c inside one() . two()内部对abc的更改对one()内部的abc的值没有影响。

This would be the case even if the arguments for two() were named a , b , and c . 即使将two()的参数命名为abc ,情况也是如此。

Here's a fiddle with the code above. 这是上面代码的摆弄



EDIT #2 编辑#2

Here is one way you could create functions that move a game object: 这是创建移动游戏对象的函数的一种方法:

var FORWARD = 0;
var BACK = 1;
var LEFT = 2;
var RIGHT = 3;

// use an object with three values to represent a position
var pos = {
    x: 0,
    y: 0,
    z: 0
};

pos = moveObject(pos, FORWARD);
printPosition(pos);

pos = moveObject(pos, LEFT);
printPosition(pos);

pos = moveObject(pos, FORWARD);
printPosition(pos);

pos = moveObject(pos, LEFT);
printPosition(pos);

// invoking moveObject() with one argument
// will move the object forward

pos = moveObject(pos);
printPosition(pos);

function moveObject(position, direction) {

    // assume FORWARD if no direction is specified
    if (typeof direction === 'undefined') {
        direction = FORWARD;
    }

    if (direction === FORWARD) {
        ++position.z;
    }
    if (direction === BACK) {
        --position.z;
    }
    if (direction === LEFT) {
        --position.x;
    }
    if (direction === RIGHT) {
        ++position.x;
    }

    return position;
}

function printPosition(pos) {
    alert(pos.x + ", " + pos.y + ", " + pos.z);
}

Here's a fiddle that shows a working demo of another approach. 这是一个小提琴 ,展示了另一种方法的可行演示。

There are two concepts that are at play here. 这里有两个概念在起作用。

1 . 1。 Variable number of function parameters (or optional parameters). 可变数量的功能参数(或可选参数)。

If you are going to call the same function with different number of parameters (this will eventually lead to a world of headache), you need to determine (inside the function) how this function was called. 如果要使用不同数量的参数调用同一函数(最终将导致头痛),则需要确定(在函数内部)如何调用此函数。 You can use arguments object available inside each function: 您可以使用每个函数内部可用的arguments对象:

function Too() {
  if (arguments.length == 4) {
    arguments[0]*arguments[0];
    arguments[1]*arguments[1];
    arguments[2]*arguments[2];
    return ++arguments[3];
  } else if (arguments.length == 1) {
    return ++arguments[0];
  } else {
    // you decide what to do here
  }
}

2 . 2。 Asynchronous code execution. 异步代码执行。

Realize that Too which is called when interval expires, executes well after One completes and returns. 意识到时间间隔到期时调用的Too ,在One完成并返回后执行良好。 If you want Too to affect newVal variable, and somehow get at this new value afterwards, - make newVal variable global. 如果你想Too影响newVal变量,并在这个新的价值某种方式得到之后, -使newVal可变全球。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 可变数量的函数参数 - JavaScript variable number of arguments to function 是否可以向 JavaScript 函数发送可变数量的参数? - Is it possible to send a variable number of arguments to a JavaScript function? 函数中可变数量的参数 - Variable number of arguments in a function Javascript:修改一个函数,打印出可递归的变量数量的参数? - Javascript: modifying a function which prints out variable number of arguments to be recursive? 带有可变数量参数的Javascript函数-更改其值-然后返回 - Javascript function that takes variable number of arguments - changes their value - and then returns them 在javascript函数参数列表中传递可变数量的参数 - Passing Variable Number of arguments in javascript function argument-list 在JavaScript中使用可变数量的参数调用函数(类似于call()) - Call a function with a variable number of arguments in JavaScript (similar to call()) 如何在不使用“参数”的情况下计算JavaScript函数的参数数量? - How to count the number of arguments to a JavaScript function without using “arguments”? 如何在1-arg javascript函数中使用2个参数? - How to use 2 arguments in a 1-arg javascript function? 具有大量参数的Javascript函数 - Javascript function with large number of arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM