javascript prototypeと_uプロト.と継承の仕組み
1364 ワード
なぜプロトタイプが現れましたか?prototypeは、共通の属性を暴露するために(相続を実現するために)、プライベートの属性は構造関数内で実現される(変数の局所性).
function DOG(name){
this.name = name;
}
DOG.prototype = { species : ' ' };
var dogA = new DOG(' ');
var dogB = new DOG(' ');
alert(dogA.species); //
alert(dogB.species); //
DOG.prototype.species = ' ';
alert(dogA.species); //
alert(dogB.species); //
// dogA dogB name dogA dogB species
プロトタイプチェーンテストlet _simple={param:1};
console.log(_simple.__proto__===Function.prototype);
console.log(_simple.__proto__===Object.prototype);
function Foo(){};//
var f1 = new Foo();//f1
console.log(f1.prototype);
//undefined prototype __proto__?
// __proto__ prototype portotype
console.log(f1.constructor===Foo.prototype.constructor);
//true prototype ==== __proto__ Foo.prototype prototype
console.log(typeof Foo);
console.log(f1.__proto__===Foo.prototype);//true
console.log(Foo.__proto__===Function.prototype);//true
console.log(Function.__proto__===Object.prototype);//true
console.log(Foo===Foo.prototype.constructor);
//Foo Foo constructor Foo.prototype.constructor Foo.prototype.constructor Foo