如何在JavaScript函数中使用可变数量的参数
当有多个参数时,我很难理解如何从第二个函数返回第一个函数的信息。现在我知道下面的代码是有效的如何在JavaScript函数中使用可变数量的参数,javascript,Javascript,当有多个参数时,我很难理解如何从第二个函数返回第一个函数的信息。现在我知道下面的代码是有效的 function One() { var newVal = 0; newVal = Too(newVal); console.log(newVal); } function Too(arg) { ++arg; return arg; } 但是,如果我试图通过添加参数和setinterval来使事情复杂化,会怎么样呢 function One() { v
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。这就是我想问的问题: 当只传递一个参数时,如何对函数的第四个参数进行操作 这个问题的答案是: 如果要对函数的第4个参数进行操作,则必须至少向函数传递4个参数 有几种不同的方法可以解决问题 #1 如果有一个参数始终是必需的,请确保它是第一个参数:
function Too(mandatoryArg, optionalArg1, optionalArg2) {
alert(++mandatoryArg);
if (optionalArg1) {
alert(++optionalArg1);
}
}
#2
传递所有未定义或未知参数的占位符值
您可以使用null
、undefined
或'
#3
根据论据的数量做出决定:
function Too(optArg1, optArg2, optArg3) {
var numArgs = arguments.length;
if (numArgs === 1) {
alert(++optArg1);
}
if (numArgs === 3) {
alert(++optArg3);
}
}
编辑
“这会更新第一个函数中的变量吗?” 让我们用一个实际的例子来说明:
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
函数1()中仅修改d
的值
这是因为d
被分配了返回值two()
对a
、b
和c
的更改在两个()内对a
、b
和c
在一个()内的值没有影响
即使two()的参数名为a
、b
和c
,情况也是如此
下面是一个与上面代码相同的示例
编辑#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);
}
这里有一个演示,展示了另一种方法的工作演示。这就是我想问的问题: 当只传递一个参数时,如何对函数的第四个参数进行操作 这个问题的答案是: 如果要对函数的第4个参数进行操作,则必须至少向函数传递4个参数 有几种不同的方法可以解决问题 #1 如果有一个参数始终是必需的,请确保它是第一个参数:
function Too(mandatoryArg, optionalArg1, optionalArg2) {
alert(++mandatoryArg);
if (optionalArg1) {
alert(++optionalArg1);
}
}
#2
传递所有未定义或未知参数的占位符值
您可以使用null
、undefined
或'
#3
根据论据的数量做出决定:
function Too(optArg1, optArg2, optArg3) {
var numArgs = arguments.length;
if (numArgs === 1) {
alert(++optArg1);
}
if (numArgs === 3) {
alert(++optArg3);
}
}
编辑
“这会更新第一个函数中的变量吗?” 让我们用一个实际的例子来说明:
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
函数1()中仅修改d
的值
这是因为d
被分配了返回值two()
对a
、b
和c
的更改在两个()内对a
、b
和c
在一个()内的值没有影响
即使two()的参数名为a
、b
和c
,情况也是如此
下面是一个与上面代码相同的示例
编辑#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);
}
下面是一个演示另一种方法的示例。这里有两个概念 一,。函数参数(或可选参数)数量可变 如果要使用不同数量的参数调用同一个函数(这最终会导致头痛),则需要确定(在函数内部)如何调用此函数。您可以在每个函数中使用可用的
参数对象:
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
}
}
二,。异步代码执行
认识到当间隔过期时调用的也会在One
完成并返回后很好地执行。如果您希望也影响newVal
变量,然后以某种方式获得此新值,则将newVal
变量设置为全局变量。这里有两个概念
一,。函数参数(或可选参数)数量可变
如果要使用不同数量的参数调用同一个函数(这最终会导致头痛),则需要确定(在函数内部)如何调用此函数。您可以在每个函数中使用可用的参数对象:
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
}
}
二,。异步代码执行
认识到当间隔过期时调用的也会在One
完成并返回后很好地执行。如果您希望也影响newVal
变量,并在之后以某种方式获取此新值,请将newVal
变量设置为全局变量。您可以使用return++arg代码>。保存一行。newVal=Too(newVal)代码>甚至不会做你想做的事。你根本没有通过X、Y或Z。为什么不这样做:newVal=Too(null,null,null,newVal)代码>我还有一些其他代码,我正在使用three.js,但是它非常长,所以我只是在这里尝试简化它。有人回答了我的问题,但现在他们建议我使用arguments.length-1或类似的东西。更多的是,我尝试从第二个函数返回信息,但有多个参数,我只想返回1。@mattedgod,所以我只需将未传递回的参数替换为null?可以使用return++arg代码>。保存一行。newVal=Too(newVal)代码>不会被删除