Say I have this block of code:
let hamster = {
stomach: [],
eat(food) {
this.stomach.push(food);
}
};
let speedy = {
__proto__: hamster
};
let lazy = {
__proto__: hamster
};
speedy.eat("apple");
alert( speedy.stomach ); // apple
.
alert( lazy.stomach ); // apple
Both speedy and lazy objects share the same stomach array, but when I instead use a simple assignment for the eat function :
eat(food) {
// assign to this.stomach instead of this.stomach.push
this.stomach = [food];
}
};
For the simple assignment hey have different stomach arrays.
Both cases behave differently but why is that? Yet both use the ‘this’ keyword.
You need to sign in to view this answers