Answer the question
In order to leave comments, you need to log in
Why doesn't an array work in a constructor?
function Hamster() {
this.food = []
}
Hamster.prototype = {
found: function(something) {
this.food.push(something)
}
}
speedy = new Hamster()
lazy = new Hamster()
speedy.found("apple")
speedy.found("orange")
console.log(speedy.food.length) // 2
console.log(lazy.food.length) // 0 (!??)
Answer the question
In order to leave comments, you need to log in
So you put 2 fruits on the speedy hamster and nothing on the lazy hamster. Lazy is nervous...
Why don't you use var?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question