類有三種屬性 公有屬性 私有屬性 靜態(tài)方法(靜態(tài)屬性)
//1.=>繼承私有
Gouzaohanshu.call(this);
//2.=>繼承公有
//1)
Zilei.prototypy.__proto__=Fulei.prototype;
//2)
Zilei.prototypy=Object.create(Fulei.prototype)
//原理實(shí)現(xiàn)
function create(parentPrototype){
function Fn(){};
Fn.prototype = parentPrototype;
return new Fn();
}
//1猎塞,2的原理本質(zhì)都是在鏈上新增一個(gè)環(huán)節(jié) 讓子類的原型的__proto__指向父類的原型
//3.=>繼承公有和私有
Zilei.prototypy = new Fulei();
///////////////////////////////////////
//Es6中的類
//類只能new不能項(xiàng)構(gòu)造函數(shù)一樣直接運(yùn)行
//類能夠繼承 公有 私有 和靜態(tài)方法
//父類的構(gòu)造函數(shù)如果返回了一個(gè)對(duì)象 會(huì)替換子類繼承來的私有屬性
class Parent{
constructor(){
this.name = '爸爸';
}
static a(){//也會(huì)被繼承
console.log(1)
}
eat(){
console.log('吃')
}
}
class Child extends Parent{//要求繼承父親的公有和私有
constructor(){//私有屬性在實(shí)例上
super();//繼承私有屬性 等價(jià)Parent.call(this)
this.age = 9;
}
static b(){//靜態(tài)方法在類上不在實(shí)例上
console.log(2)
}
smoking(){//公有屬性在實(shí)例上
console.log('抽煙')
}
}
let child = new Child();
Child.a();
//Es6類的實(shí)現(xiàn)
//類的調(diào)用檢測(cè) 看看是不是new出來的
function _classCallCheak(instance,constructor){
if(!(instance instanceof constructor)){
throw new Error('大兄弟夕冲,你沒有new')
}
}
let Parent = (function(){
function P(){
_classCallCheak(this,P)
}
return P;
}());
Parent()