1均抽、借助原型是實現(xiàn)繼承
實現(xiàn)原理: 讓子類的原型對象指向父類實例尚洽,當(dāng)子類實例找不到對應(yīng)的屬性和方法時夷家,就會往它的原型對象,也就是父類實例上找帝美,從而實現(xiàn)對父類的屬性和方法的繼承
function Parent() {
this.name = "parent"
this.arr = [1, 2, 3]
}
function Child() {
this.type = "child"
}
Child.prototype = new Parent()
var c1 = new Child()
var c2 = new Child()
缺點(diǎn)一碍彭、所有實例都會共享父類實例的屬性。(原型上的屬性是共享的悼潭,一個實例修改了原型屬性庇忌,另一個實例的原型屬性也會被修改!=⑼省)
c1.arr.push(4)
console.log(c1, c2)
缺點(diǎn)二皆疹、在創(chuàng)建子類型的實例時,無法向父類型的構(gòu)造函數(shù)傳遞參數(shù)占拍。
2略就、構(gòu)造函數(shù)實現(xiàn)繼承
實現(xiàn)原理: 在子類的構(gòu)造函數(shù)中執(zhí)行父類的構(gòu)造函數(shù),并為其綁定子類的this晃酒,讓父類的構(gòu)造函數(shù)把成員屬性和方法都掛到子類this上去 ( call表牢、apply )
function Parent() {
this.name = ["LB", "ipanda"];
}
function Child() {
Parent.call(this)
this.type = "child"
}
解決了原型鏈繼承 所有實例都會共享父類實例的屬性的缺點(diǎn)
let c1 = new Child()
c1.name.push("babay")
console.log(c1.name);
let c2 = new Child()
console.log(c2.name);
解決了原型鏈繼承 在創(chuàng)建子類型的實例時,無法向父類型的構(gòu)造函數(shù)傳遞參數(shù)贝次。
function Parent(name) {
this.name = name;
}
function Child(name) {
Parent.call(this, name)
}
let c1 = new Child(["LB", "ipanda"])
c1.name.push("babay")
console.log(c1.name);
缺點(diǎn)一崔兴、只繼承了父類構(gòu)造函數(shù)的屬性,沒有繼承父類原型的屬性
function Parent() {
this.name = ["LB", "ipanda"];
}
Parent.prototype.myFunc = function () { }
function Child() {
Parent.call(this)
}
let c1 = new Child()
console.log(c1.name);
console.log(c1.myFunc);
缺點(diǎn)二浊闪、無法實現(xiàn)父類構(gòu)造函數(shù)的復(fù)用恼布,在創(chuàng)建子類型的實例時都要調(diào)用父類構(gòu)造函數(shù)
let num = 0;
function Parent() {
console.log(`被調(diào)用次數(shù)${++num}`);
}
function Child() {
Parent.call(this)
}
let c1 = new Child()
let c2 = new Child()
let c3 = new Child()
缺點(diǎn)三螺戳、每個子類實例都會拷貝一份父類構(gòu)造函數(shù)中的方法搁宾,作為實例自己的方法,比如 myFunc()
- 每個實例都拷貝一份倔幼,占用內(nèi)存大盖腿,尤其是方法過多的時候。
- 方法都作為了實例自己的方法损同,當(dāng)需求改變翩腐,要改動其中的一個方法時,之前所有的實例膏燃,他們的該方法都不能及時作出更新茂卦。只有后面的實例才能訪問到新方法。
function Parent() {
this.nameList = ["LB", "ipanda"];
this.myFunc = function(){
console.log("Parent !!!");
}
}
function Child() {
Parent.call(this)
}
let c1 = new Child()
console.log(c1);
3组哩、組合方式實現(xiàn)繼承
結(jié)合了兩種模式的優(yōu)點(diǎn):使用借用構(gòu)造函數(shù)的技術(shù)實現(xiàn)實例屬性的繼承等龙,使用原型鏈實現(xiàn)原型屬性和方法的繼承处渣。
function Parent() {
this.name = "parent"
this.arr = [1, 2, 3]
}
Parent.prototype.myFunc = function(){
console.log("MyFunction");
}
function Child() {
Parent.call(this)
this.type = "child"
}
Child.prototype = new Parent()
var c1 = new Child()
c1.arr.push(4)
console.log(c1);
缺點(diǎn):(1)調(diào)用了兩次父類構(gòu)造函數(shù)(耗內(nèi)存),一次是在創(chuàng)建子類型原型時蛛砰,另一次是在子類型構(gòu)造函數(shù)內(nèi)部罐栈;(2)創(chuàng)建的實例和原型上存在兩份相同的屬性(耗內(nèi)存)
let num = 0;
function Parent() {
console.log(`被調(diào)用次數(shù)${++num}`);
this.name = "parent"
this.arr = [1, 2, 3]
}
Parent.prototype.myFunc = function(){
console.log("MyFunction");
}
function Child() {
Parent.call(this)//第二次調(diào)用
this.type = "child"
}
Child.prototype = new Parent()//第一次調(diào)用
Child.prototype.construcotr = Child
var c1 = new Child()
c1.arr.push(4)
console.log(c1);
3、寄生組合式繼承
let num = 0;
function Parent() {
console.log(`被調(diào)用次數(shù)${++num}`);
this.name = "parent"
this.arr = [1, 2, 3]
function hello(){
console.log("hello")
}
}
Parent.prototype.myFunc = function () {
console.log("MyFunction");
}
function Child() {
Parent.call(this)
this.type = "child"
}
Child.prototype = Object.create(Parent.prototype)
Child.prototype.construcotr = Child
var c1 = new Child()
c1.arr.push(4)
console.log(c1)
ES6的繼承
class People{
constructor(name,age){
this.name = name
this.age = age
}
eat(){
}
}
class Student extends People{
constructor(id,name,age){
super(name,age)
this.id = id
}
}