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().
No, if you want to use the values that were sent to
Placemat(), you need to reference them in thedeal()andhit()functions. Not list them as parameters to those functions.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 theidandid2parameters.But if those nested functions define their own
idorid2parameter, then that is what will be referenced in those functions. You will have effectively shadowed the parameters in the outer function.