3

This is my code:

var Placemat = function(id,id2) {
    this.hand = document.getElementById(id);
    this.bucket = "";
    this.bucketspace = document.getElementById(id2);
    this.bucketvalue = 0;
    var that = this;
    this.deal = function(id) {
        shuffle();
        var card1 = deck.shift();
        var card2 = deck.shift();
        this.hand.innerHTML = card1 + ", " + card2;
    };
    this.hit = function(id2) {
        var card3 = deck.shift();
        this.bucket = this.bucket + deck.shift();
        this.bucketspace.innerHTML = this.bucket; 
    };
};

Is this the proper way to pass parameters to a nested function? This id and id2 in this.deal() and this.hit() are from Placemat().

1
  • I don't see what the arguments are for? Commented Aug 10, 2011 at 19:22

2 Answers 2

3

No, if you want to use the values that were sent to Placemat(), you need to reference them in the deal() and hit() functions. Not list them as parameters to those functions.

 // removed---------v
this.deal = function() {
    alert( id );  // <---- do something with the original argument
    shuffle();
    var card1 = deck.shift();
    var card2 = deck.shift();
    this.hand.innerHTML = card1 + ", " + card2;
};

 // removed--------v
this.hit = function() {
    alert( id2 );  // <---- do something with the original argument
    var card3 = deck.shift();
    this.bucket = this.bucket + deck.shift();
    this.bucketspace.innerHTML = this.bucket; 
};

Remember, the parameter is just an identifier for whatever may be passed to the function. The argument is what was actually passed.

You can reference the argument via the parameter you defined. So to have your functions reference those arguments sent to Placemat(), you can do so via the id and id2 parameters.

But if those nested functions define their own id or id2 parameter, then that is what will be referenced in those functions. You will have effectively shadowed the parameters in the outer function.

Sign up to request clarification or add additional context in comments.

Comments

1

No. In fact, what are you passing the id's in the first place? You don't use them in the functions.

The way you are doing it, you are creating two functions (hit and deal) which expect one argument each. Those arguments just happen to be named the same as the arguments to your outer function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.