1哈雏、原型鏈
/*
*?土浸?問題
*(1)父類中的**引用類型值會(huì)被子類共享**
*(2)不能向超類的構(gòu)造函數(shù)中傳遞參數(shù)
*/
function SuperType(){
this.property=true;
}
SuperType.prototype.getSuperValue=function(){
return this.property;
}
function SubType(){
this.subProperty=false;
}
//繼承SuperType,E砀!E梢蟆愈腾!先寫
SubType.prototype=new SuperType();
//然后定義方法
SubType.prototype.getSubValue=function(){
return this.subProperty;
}
//重寫超類中的方法
SubType.prototype.getSuperValue=function(){
return false;
}
var instance=new SubType();
console.log(instance.getSuperValue());
function A(a){
this.varA = a;
}
// 以上函數(shù) A 的定義中岂津,既然 A.prototype.varA 總是會(huì)被 this.varA 遮蔽,
// 那么將 varA 加入到原型(prototype)中的目的是什么橱乱?
A.prototype = {
varA : null, // 既然它沒有任何作用粱甫,干嘛不將 varA 從原型(prototype)去掉?
// 也許作為一種在隱藏類中優(yōu)化分配空間的考慮危纫?
// https://developers.google.com/speed/articles/optimizing-javascript#Initializing instance variables
// 將會(huì)驗(yàn)證如果 varA 在每個(gè)實(shí)例不被特別初始化會(huì)是什么情況。
doSomething : function(){
// ...
}
}
function B(a, b){
A.call(this, a);
this.varB = b;
}
B.prototype = Object.create(A.prototype, {
varB : {
value: null,
enumerable: true,
configurable: true,
writable: true
},
doSomething : {
value: function(){ // override
A.prototype.doSomething.apply(this, arguments); // call super
// ...
},
enumerable: true,
configurable: true,
writable: true
}
});
B.prototype.constructor = B;
var b = new B();
b.doSomething();
2契耿、借用構(gòu)造函數(shù)
/*
*優(yōu)勢(shì):可以通過構(gòu)造函數(shù)向子類中傳遞參數(shù)
*螃征??問題
*(1)函數(shù)的復(fù)用無從談起
*(2)在超類中定義的原型方法在子類中是不可見的
*/
function SuperType(name){
this.name=name;
}
function SubType(){
SuperType.call(this,"Nick");//!!!先調(diào)用踢械,防止覆蓋子類中的屬性
this.age=29;
}
var instance=new SubType();
console.log(instance.age);
console.log(instance.name);
3魄藕、組合繼承
/*
* ******最常用方式******
*缺點(diǎn):調(diào)用兩次構(gòu)造函數(shù)
*/
function SuperType(name){
this.name=name;
this.colors=["red","blue","black"];
}
SuperType.prototype.sayName=function(){
console.log(this.name);
}
function SubType(name,age){
//繼承屬性背率,必須先寫,方式被覆蓋
SuperType.call(this,name);//第二次調(diào)用
this.age=age;
}
//繼承SuperType,M松;嵊汀!翻翩!先寫
SubType.prototype=new SuperType();//第一次調(diào)用
SubType.prototype.constructor=SubType;
SubType.prototype.sayAge=function(){
console.log(this.age);
}
var instance1=new SubType("Nick",24);
instance1.colors.push("heh");
console.log(instance1.colors);
instance1.sayAge();
instance1.sayName();
var instance2=new SubType("xiaoming",33);
console.log(instance2.colors);
instance2.sayAge();
instance2.sayName();
4嫂冻、原型繼承
/*
*目的:基于已有對(duì)象創(chuàng)建新對(duì)象,同時(shí)還不必因此創(chuàng)建自定義類型
*優(yōu)點(diǎn):不用創(chuàng)建構(gòu)造函數(shù)
*缺點(diǎn):會(huì)共享相應(yīng)的值
*
*/
function object(o){
function F(){}
F.prototype=o;
return new F();
}
var Person={
name:"Nick",
friends:["gic","fd","fff"]
};
//var anotherPerson=object(Person);
//*****!!!!等價(jià)于
var anotherPerson=Object.create(Person,{
name:{
writable:true,
value:"Grey"
}
});
anotherPerson.name="Grey";
anotherPerson.friends.push("Bob");
var yetanotherPerson=object(Person);
yetanotherPerson.name="Linda";//不會(huì)修改父類的值睛低,會(huì)重新生成一個(gè)name屬性
anotherPerson.name="ff";
console.log(yetanotherPerson.name);//Linda
console.log(Person.friends);
5服傍、寄生式繼承
/*
*缺點(diǎn):函數(shù)不能復(fù)用降低效率
*主要考慮對(duì)象而不是構(gòu)造函數(shù)的情況下適用
*/
function object(o){
function F(){}
F.prototype=o;
return new F();
}
function createAnother(original){
var clone=object(original);
clone.syaHi=function(){
console.log("hi");
};
return clone;
}
var person={
name:"Nick",
friends:["gic","fd","fff"]
};
var anotherPerson=createAnother(person);
anotherPerson.syaHi();
3吹零、寄生組合繼承
/*
* 解決了組合繼承兩次調(diào)用構(gòu)造函數(shù)問題
*/
function object(o){
function F(){}
F.prototype=o;
return new F();
}
function inheritPrototype(subType,superType){
var prototype=object(superType.prototype);//創(chuàng)建對(duì)象
prototype.constructor=subType;//增強(qiáng)對(duì)象
subType.prototype=prototype;//指定對(duì)象
}
function SuperType(name){
this.name=name;
this.colors=["red","blue","black"];
}
SuperType.prototype.sayName=function(){
console.log(this.name);
}
inheritPrototype(SubType,SuperType);
function SubType(name,age){
//繼承屬性,必須先寫套蒂,方式被覆蓋
SuperType.call(this,name);//第二次調(diào)用
this.age=age;
}
SubType.prototype.sayAge=function(){
console.log(this.age);
}
var instance1=new SubType("Nick",24);
instance1.colors.push("heh");
console.log(instance1.colors);
instance1.sayAge();
instance1.sayName();
var instance2=new SubType("xiaoming",33);
console.log(instance2.colors);
instance2.sayAge();
instance2.sayName();
proto
1.所有構(gòu)造器/函數(shù)的__proto__都指向Function.prototype,
(1) 它是一個(gè)空函數(shù)(Empty function)
Number.__proto__ === Function.prototype // true
Boolean.__proto__ === Function.prototype // true
String.__proto__ === Function.prototype // true
Object.__proto__ === Function.prototype // true
Function.__proto__ === Function.prototype // true
Array.__proto__ === Function.prototype // true
RegExp.__proto__ === Function.prototype // true
Error.__proto__ === Function.prototype // true
Date.__proto__ === Function.prototype // true
包括自定的
// 函數(shù)聲明
function Person() {}
// 函數(shù)表達(dá)式
var Man = function() {}
console.log(Person.__proto__ === Function.prototype) // true
console.log(Man.__proto__ === Function.prototype) // true
js的解析器對(duì)函數(shù)聲明與函數(shù)表達(dá)式并不是一視同仁地對(duì)待的伸辟。
對(duì)于函數(shù)聲明馍刮,js解析器會(huì)優(yōu)先讀取,確保在所有代碼執(zhí)行之前
聲明已經(jīng)被解析静稻,而函數(shù)表達(dá)式匈辱,如同定義其它基本類型的變量
一樣,只在執(zhí)行到某一句時(shí)也會(huì)對(duì)其進(jìn)行解析押搪,所以在實(shí)際中浅碾,
它們還是會(huì)有差異的,具體表現(xiàn)在厦画,當(dāng)使用函數(shù)聲明的形式來定義函數(shù)時(shí)滥朱,
可將調(diào)用語句寫在函數(shù)聲明之前,而后者排嫌,這樣做的話會(huì)報(bào)錯(cuò)缰犁。
(2)Math,JSON是以對(duì)象形式存在的薇芝,無需new丰嘉。它們的__proto__是
Object.prototype嚷缭。
Math.__proto__ === Object.prototype // true
JSON.__proto__ === Object.prototype // true
(3)Function.prototype也是唯一一個(gè)typeof XXX.prototype為“function”
的prototype耍贾。其它的構(gòu)造器的prototype都是一個(gè)對(duì)象路幸。如下
console.log(typeof Function.prototype) // function
console.log(typeof Object.prototype) // object
console.log(typeof Number.prototype) // object
console.log(typeof Boolean.prototype) // object
console.log(typeof String.prototype) // object
console.log(typeof Array.prototype) // object
console.log(typeof RegExp.prototype) // object
console.log(typeof Error.prototype) // object
console.log(typeof Date.prototype) // object
console.log(typeof Object.prototype) // object
2.所有對(duì)象的__proto__都指向其構(gòu)造器的prototype
p.__proto__ === Person.prototype===p.constructor.prototype