简体   繁体   中英

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.

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. I only want to return the newVal not 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.

There are a few ways you can approach your problem differently.

#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

Pass placeholder values for all the undefined or unknown arguments.

You might use null , undefined , or '' .

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

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

#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:

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

Only the value of d is modified in function one() .

That's because d is assigned the return value of two() .

The changes to a , b , and c , inside two() have no effect on the values of a , b , and c inside one() .

This would be the case even if the arguments for two() were named a , b , and c .

Here's a fiddle with the code above.



EDIT #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 . 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:

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 . Asynchronous code execution.

Realize that Too which is called when interval expires, executes well after One completes and returns. If you want Too to affect newVal variable, and somehow get at this new value afterwards, - make newVal variable global.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM