Answer the question
In order to leave comments, you need to log in
Why does this code work the way it does?
I understand correctly? When a rabbit is created, will its __proto__ be set to a reference to the Rabbit.prototype object?
function Rabbit() {}
Rabbit.prototype = {
eats: true
};
var rabbit = new Rabbit();
Rabbit.prototype = {}; //*
alert( rabbit.eats );
function Rabbit(name) {}
Rabbit.prototype = {
eats: true
};
var rabbit = new Rabbit();
Rabbit.prototype.eats = false;
alert( rabbit.eats );
Answer the question
In order to leave comments, you need to log in
Why does the alert output true when overwriting this object with an empty object? because __proto__ refers to an already empty object
var obj = {
eats: true
}
function Rabbit(name) {}
Rabbit.prototype = obj
var rabbit = new Rabbit();
alert(Rabbit.prototype == obj); // true
alert(rabbit.__proto__ == obj); // true
alert(rabbit.__proto__ == Rabbit.prototype); // true, т.к. у обоих ссылка на один и тот же объект.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question