I have a prototype function which I use to make two other functions with Object.create()
. The private variables, however, are shared. How can I make each instance have its own instance of the variables?
var testCodePrototype = (function(){
var score = 0;
this.updateScore= function(amount){
score += amount;
return "Score updated to " + score;
};
this.getScore = function(){
return score;
};
return {
updateScore,
getScore,
};
}());
var test1 = Object.create(testCodePrototype, {
name: {value: function(type){
return "I am test1";
}}
});
var test2 = Object.create(testCodePrototype, {
name: {value: function(type){
return "I am second.";
}}
});
console.log(test1.name());
console.log(test2.name());
console.log(test1.updateScore(5));
console.log(test1.getScore()); // returns 5
console.log(test2.getScore()); // returns 5!!