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

标签 javascript

当有多个参数时,我无法理解如何将信息从第二个函数返回到第一个函数。现在我知道以下代码有效。

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

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

但是,如果我试图通过添加参数和 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;
}

我不确定如何处理 newVal = 代码行。我只想返回 newVal 而不是 x,y,z。

最佳答案

这就是我想你想问的:

How can I operate on the 4th argument to a function when only one argument is passed?

这个问题的答案是这样的:

If you want to operate on the 4th argument of a function, at least 4 arguments must be passed to the function.

有几种不同的方法可以解决您的问题。

#1

如果有一个参数总是必要的,确保它是第一个参数:

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

#2

为所有未定义或未知参数传递占位符值。

您可能会使用 null , undefined , 或 '' .

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

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

#3

根据参数的数量做出决定:

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


编辑

"Will this update a variable in the first function?"

让我们用一个实际的例子来演示:

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

调用 one()将导致以下警报:

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

只有d的值在函数 one() 中修改。

那是因为d被赋予 two() 的返回值。

a 的更改, b , 和 c , 在 two() 内部对 a 的值没有影响, b , 和 cone() 中。

即使 two() 的参数被命名为 a 也是如此。 , b , 和 c .

这是一个 fiddle使用上面的代码。



编辑 #2

这是您可以创建移动游戏对象的函数的一种方法:

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

这是一个 fiddle展示了另一种方法的工作演示。

关于javascript - 如何在 JavaScript 函数中使用可变数量的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19439937/

相关文章:

javascript - jQuery 遍历列表项并获取 data-imgid 属性值

javascript - rxjs 进行的限速 http 调用

javascript - 无法将 ng-repeat 与来自 ajax 调用的 JSON 一起使用

java - HTML重定向

javascript - 回调函数内的变量作用域怎么样

javascript - 正则表达式 - 如何在关键字最后一次出现后检索单词

javascript - AngularJS:如何使用 $location.path() 发送参数

javascript - 脚本 600 错误 : Invalid target element for this operation

JavaScript 日期月份

javascript - props.onChange(e.target.value) 返回一个对象,而不是 Material ui 中 textField 中的值