( new F() ).
F.prototype new [[Prototype]] .
.
"prototype" . .
F.prototype "prototype" F. .
:
let animal = {
eats: true
};
function Rabbit(name) {
this.name = name;
}
Rabbit.prototype = animal; //
let rabbit = new Rabbit("White Rabbit"); // rabbit.__proto__ == animal
alert( rabbit.eats ); // true
Rabbit.prototype = animal : new Rabbit [[Prototype]] animal.
:
[proto-constructor-animal-rabbit.png]
"prototype" ( ) [[Prototype]] ( rabbit animal).
F.prototype new F [[Prototype]].
F.prototype (F.prototype = <another object>) [[Prototype]] ( )
.
prototype
"prototype" .
"prototype" .
:
function Rabbit() {}
/* prototype
Rabbit.prototype = { constructor: Rabbit };
*/
[function-prototype-constructor.png]
:
function Rabbit() {}
// :
// Rabbit.prototype = { constructor: Rabbit }
alert( Rabbit.prototype.constructor == Rabbit ); // true
constructor rabbit [[Prototype]]:
function Rabbit() {}
// :
// Rabbit.prototype = { constructor: Rabbit }
let rabbit = new Rabbit(); // {constructor: Rabbit}
alert(rabbit.constructor == Rabbit); // true prototype
[rabbit-prototype-constructor.png]
constructor .
:
function Rabbit(name) {
this.name = name;
alert(name);
}
let rabbit = new Rabbit("White Rabbit");
//
let rabbit2 = new rabbit.constructor("Black Rabbit");
( ) .
"constructor" "constructor".
"prototype" . .
prototype "constructor".
:
function Rabbit() {}
Rabbit.prototype = {
jumps: true
};
let rabbit = new Rabbit();
//
alert(rabbit.constructor === Rabbit); // false
"constructor" "prototype" . :
function Rabbit() {}
// Rabbit.prototype
//
Rabbit.prototype.jumps = true
// Rabbit.prototype.constructor
( ) constructor :
Rabbit.prototype = {
jumps: true,
constructor: Rabbit //
};
//
[[Prototype]] . .
:
-
F.prototype([[Prototype]])[[Prototype]]new F(). -
F.prototypenull. -
"prototype"new.
:
let user = {
name: "John",
prototype: "Bla-bla" //
};
F.prototype = { constructor: F } "constructor" .
prototype
: 5
new Rabbit prototype .
:
function Rabbit() {}
Rabbit.prototype = {
eats: true
};
let rabbit = new Rabbit();
alert( rabbit.eats ); // true
-
( ).
alertfunction Rabbit() {} Rabbit.prototype = { eats: true }; let rabbit = new Rabbit(); Rabbit.prototype = {}; // (*) alert( rabbit.eats ); // ? -
( )
function Rabbit() {} Rabbit.prototype = { eats: true }; let rabbit = new Rabbit(); Rabbit.prototype.eats = false; // (*) alert( rabbit.eats ); // ? -
( )
function Rabbit() {} Rabbit.prototype = { eats: true }; let rabbit = new Rabbit(); delete rabbit.eats; // (*) alert( rabbit.eats ); // ? -
:
function Rabbit() {} Rabbit.prototype = { eats: true }; let rabbit = new Rabbit(); delete Rabbit.prototype.eats; // (*) alert( rabbit.eats ); // ?
:
-
true.Rabbit.prototype[[Prototype]]. -
false.Rabbit.prototypeRabbit.prototypeRabbit.prototype[[Prototype]]rabbit..
-
true. .delete rabbit.eatsrabbit. . -
undefined.eatsprototype .
: 5
obj .
let obj2 = new obj.constructor();
obj .
"constructor" .
"prototype" :
function User(name) {
this.name = name;
}
let user = new User('John');
let user2 = new user.constructor('Pete');
alert( user2.name ); // Pete (!)
User.prototype.constructor == User.
User.prototype constructor User .
:
function User(name) {
this.name = name;
}
User.prototype = {}; // (*)
let user = new User('John');
let user2 = new user.constructor('Pete');
alert( user2.name ); // undefined
user2.name undefined
new user.constructor('Pete'):
-
constructoruser. - prototype prototype
userUser.prototype. -
User.prototype{}prototypeObject.prototypeObject.prototype.constructor == Object.
let user2 = new Object('Pete') Object . let user2 = {} user2 .
<code><pre>10 (plnkr, JSBin, codepen)