, -, new F().
F.prototype , new [[Prototype]] .
JavaScript . .
, , . prototype -, . .
, F.prototype , "prototype" F. prototype, .
:
let animal = {
eats: true
};
function Rabbit(name) {
this.name = name;
}
Rabbit.prototype = animal;
let rabbit = new Rabbit(" "); // rabbit.__proto__ == animal
alert( rabbit.eats ); // true
Rabbit.prototype = animal : new Rabbit, [[Prototype]] animal.
:
, "prototype" , [[Prototype]], , rabbit animal.
F.prototype - new FF.prototype new F [[Prototype]] .
F.prototype (F.prototype = < '>), , new F, [[Prototype]] , .
F.prototype,
"prototype", .
prototype constructor, .
:
function Rabbit() {}
/* prototype,
Rabbit.prototype = { constructor: Rabbit };
*/
:
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 ( )
constructor -, .
:
function Rabbit(name) {
this.name = name;
alert(name);
}
let rabbit = new Rabbit(" ");
let rabbit2 = new rabbit.constructor(" ");
, ( ), , .
"constructor" ,
JavaScript "constructor".
, constructor "prototype" , . "constructor" .
, prototype , prototype "constructor".
:
function Rabbit() {}
Rabbit.prototype = {
jumps: true
};
let rabbit = new Rabbit();
alert(rabbit.constructor === Rabbit); // false
, "constructor", prototype , "prototype":
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)