This is my code:
var shuffle = function(x) {
var deck = [];
deck.push(x);
return deck;
};
var Placemat = function() {
var card1 = deck.shift();
var card2 = deck.shift();
}
By returning deck in shuffle() like this, can I use it in Placemat()? If not, how can I do it?
Yes you could. But anytime you call
.shuffle()you would overwrite that array, or more precisely, create a new instance. So if you want to keep that reference you could go likeNow
.shuffle()closes over thedeckby returning another function. So it could look likeEven if that is probably not the greatest way to go. But I guess I don’t even know what exactly you want to achieve there.