简体   繁体   中英

Calling a function without entering its parameters in Javascript

I am trying to create a JavaScript version of an old timey dice game named PIG. It involves rolling dice and keeping track of the sum of each roll. I need to be able to save each roll within a function and then be able to call said function to return the current roll score.


This is the method that takes the amount of the current roll and stores it into the variable total, subsequently returning the current total variable.

  function rollTotal(amount) {
  return amount;
  }

I first call the function and insert the score of the dice roll into the amount parameter...

var dieOne = 1;
var dieTwo = 2;
rollTotal(dieOne + dieTwo);

Then I call the function to return the current score like this...

rollTotal()

and am getting this...

undefined

Is there a way to set a default parameter to return the current score if a parameter is not entered when called?

When you call a function and don't supply a parameter, then the parameter is bound to undefined within the function. So you can do something like this:

var currentAmount = undefined; // or perhaps 0?
function rollTotal(amount) {
    if (typeof(amount) === 'undefined') {
        return currentAmount;
    } else {
        currentAmount = amount;
        return amount;
    }
}

You can do this more elegantly (and more safely) using a closure:

var rollTotal = (function() {
    var currentAmount = undefined;
    return function(amount) {
        if (typeof(amount) === 'undefined') {
            return currentAmount;
        } else {
            currentAmount = amount;
            return amount;
        }
    };
}());

You might also want to do a more robust test of the argument and treat any non-number as undefined. You can do this with the following test:

!isNaN(parseFloat(amount)) && isFinite(amount)

instead of testing specifically for undefined .

EDIT: As @Paul S. points out, the best test as to whether an argument was passed is arguments.length === 0 .

Why use a function? The = operator works just fine.

// initial setup
var rollTotal;

// set in the middle of an expression
"You just rolled " + (rollTotal = dieOne + dieTwo) + "!";

// recalling later in an expression
"Last time you rolled " + rollTotal + ", what will you get this time?";

A popular way to do this is with an instance of a class.

That is:

function Dice() {
    var amount;

    this.rollTotal = function(_amount) {
        // if an argument is supplied, assign it to the internal amount
        if (typeof _amount !== "number") {
            amount = _amount;
        }
        return amount;
    };
}

Example usage:

var dice = new Dice();
dice.rollTotal(3);
console.log(dice.rollTotal());

In JavaScript you can look at the arguments object to check whether an argument was passed or not when the function was called. Here is how you can check if any argumemt was passed upon calling the function.

function rollTotal(amount){
  if(arguments.length === 0)
     return (a value when no arguments were passed);
  else return amount;
}

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