, - new F().
F.prototype , new [[Prototype]] .
JavaScript . .
, , . "prototype" -, . .
, F.prototype "prototype" F. , 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() animal [[Prototype]].
:
: "prototype" , "F", [[Prototype]] , rabbit animal.
F.prototype new FF.prototype new F [[Prototype]] .
F.prototype (F.prototype = < >), , new F, [[Prototype]] , .
F.prototype , constructor
( ) "prototype".
"prototype" constructor, -.
:
function Rabbit() {}
/*
Rabbit.prototype = { constructor: Rabbit };
*/
:
function Rabbit() {}
// :
// Rabbit.prototype = { constructor: Rabbit }
alert( Rabbit.prototype.constructor == Rabbit ); // true
, , constructor [[Prototype]]:
function Rabbit() {}
// :
// Rabbit.prototype = { constructor: Rabbit }
let rabbit = new Rabbit(); // {constructor: Rabbit}
alert(rabbit.constructor == Rabbit); // true ( )
constructor .
:
function Rabbit(name) {
this.name = name;
alert(name);
}
let rabbit = new Rabbit("White Rabbit");
let rabbit2 = new rabbit.constructor("Black Rabbit");
, , , (, ), .
, , "constructor" ,
JavaScript "constructor".
, "prototype" , .
, , "constructor" .
:
function Rabbit() {}
Rabbit.prototype = {
jumps: true
};
let rabbit = new Rabbit();
alert(rabbit.constructor === Rabbit); // false
, "constructor", // , :
function Rabbit() {}
// Rabbit.prototype ,
//
Rabbit.prototype.jumps = true
// , Rabbit.prototype.constructor
constructor:
Rabbit.prototype = {
jumps: true,
constructor: Rabbit
};
// constructor ,
[[Prototype]] , -. , .
. :
-
F.prototype([[Prototype]])[[Prototype]]new F(). -
F.prototype,null. . -
"prototype", -,new.
prototype - :
let user = {
name: "John",
prototype: "Bla-bla" // -
};
F.prototype = { constructor: F }, "constructor".
<code>, —<pre>, 10 — (plnkr, JSBin, codepen)