JS繼承

1:原型繼承

為了讓子類繼承父類的屬性(也包括方法),首先需要定義一個(gè)構(gòu)造函數(shù)。然后,將父類的新實(shí)例賦值給構(gòu)造函數(shù)的原型。代碼如下:

eg:原型繼承

//全是改變的指針模擬繼承 new this指針改變 通過綁定prototype復(fù)制指針改變
function proto(){
this.name="caoshiyuan"http://如果this不懂看下一個(gè)類式繼承
}
function child(){
this.age=23
}
function generator(){
var name=123
}
console.log(child.constructor)
console.log(proto.constructor)
console.log(generator.constructor())
console.log(generator.constructor)
// ------------以上可以略過
child.prototype=new proto()
let childs=new child()
console.log(childs.name)//caoshiyuan
console.log(childs)//沒有name
console.log(childs.proto)//只有name指向原型 proto指向?qū)嵗瘮?shù)的原型 他的原型是一個(gè)指針指向proto函數(shù)
console.log(child.prototype)//和上面相等

2:類式繼承

主要使用call apply 方法實(shí)現(xiàn)繼承
1、方法定義
call方法: 語法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定義:調(diào)用一個(gè)對象的一個(gè)方法,以另一個(gè)對象替換當(dāng)前對象。 說明: call 方法可以用來代替另一個(gè)對象調(diào)用一個(gè)方法。call 方法可將一個(gè)函數(shù)的對象上下文從初始的上下文改變?yōu)橛?thisObj 指定的新對象。 如果沒有提供 thisObj 參數(shù),那么 Global 對象被用作 thisObj。 apply方法: 語法:apply([thisObj[,argArray]]) 定義:應(yīng)用某一對象的一個(gè)方法,用另一個(gè)對象替換當(dāng)前對象。 說明: 如果 argArray 不是一個(gè)有效的數(shù)組或者不是 arguments 對象,那么將導(dǎo)致一個(gè) TypeError。 如果沒有提供 argArray 和 thisObj 任何一個(gè)參數(shù),那么 Global 對象將被用作 thisObj, 并且無法被傳遞任何參數(shù)。

eg: call

function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}

add.call(sub,3,1);

這個(gè)例子中的意思就是用 add 來替換 sub,add.call(sub,3,1) == add(3,1) ,所以運(yùn)行結(jié)果為:alert(4); // 注意:js 中的函數(shù)其實(shí)是對象,函數(shù)名是對 Function 對象的引用。

b、
function Animal(){
this.name = "Animal";
this.showName = function(){
alert(this.name);
}
}

function Cat(){
this.name = "Cat";
}

var animal = new Animal();
var cat = new Cat();

//通過call或apply方法,將原本屬于Animal對象的showName()方法交給對象cat來使用了。
//輸入結(jié)果為"Cat"
animal.showName.call(cat,",");
//animal.showName.apply(cat,[]);

call 的意思是把 animal 的方法放到cat上執(zhí)行,原來cat是沒有showName() 方法,現(xiàn)在是把a(bǔ)nimal 的showName()方法放到 cat上來執(zhí)行,所以this.name 應(yīng)該是 Cat

c、實(shí)現(xiàn)繼承
function Animal(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}

function Cat(name){
Animal.call(this, name);
}

var cat = new Cat("Black Cat");
cat.showName();

Animal.call(this) 的意思就是使用 Animal對象代替this對象,那么 Cat中不就有Animal的所有屬性和方法了嗎,Cat對象就能夠直接調(diào)用Animal的方法以及屬性了.

d、多重繼承
function Class10()
{
this.showSub = function(a,b)
{
alert(a-b);
}
}

function Class11()
{
this.showAdd = function(a,b)
{
alert(a+b);
}
}

function Class2()
{
Class10.call(this);
Class11.call(this);
}

很簡單,使用兩個(gè) call 就實(shí)現(xiàn)多重繼承了當(dāng)然,js的繼承還有其他方法,例如使用原型鏈,這個(gè)不屬于本文的范疇,只是在此說明call 的用法。說了call ,當(dāng)然還有 apply,這兩個(gè)方法基本上是一個(gè)意思,區(qū)別在于 call 的第二個(gè)參數(shù)可以是任意類型,而apply的第二個(gè)參數(shù)必須是數(shù)組,也可以是arguments還有 callee,caller.
上面實(shí)在看不懂在下謝了一些小例子

eg://類式繼承

function Super(){
this.colors=["red","blue"];
}
function Sub(){
Super.call(this);//這里利用call更改this指向
}
console.log(Sub)
console.log(Sub.prototype)//空的證明了什么?空的證明了什么?
let sub=new Sub()
console.log(sub.colors)

//call
// function add(a,b){
// alert(a+b)
// }
// function cal(a,b){
// alert(a-b)
// }
// add.call(cal,2,3)

function add1(a,b){
alert(a+b)
}
function cal1(a,b){

add1.call(this,a,b)//this 后面的值是傳入的參數(shù) apply是數(shù)組形式

}
let add11=new cal1(1,2).

組合繼承 原型+類式

// 組合繼承 原型一般都是放的方法

function Parent(age){
this.name = ['mike','jack','smith'];
this.age = age;
}
Parent.prototype.run = function () {
return this.name + ' are both' + this.age;
};
function Child(age){
Parent.call(this,age);//對象冒充,給超類型傳參 } Child.prototype = new Parent();//原型鏈繼承
var test = new Child(21);//寫new Parent(21)也行 alert(test.run());//mike,jack,smith are both21
}

//寄生模式
// 這種繼承方式是把原型式+工廠模式結(jié)合起來,目的是為了封裝創(chuàng)建的過程。

function create(o){
    var f= obj(o);
    f.run = function () {
        return this.arr;//同樣,會共享引用
    };
    return f;
}

//寄生式組合

function obj(o){
    function F(){}
    F.prototype = o;
    return new F();
}
function create(parent,test){
    var f = obj(parent.prototype);//創(chuàng)建對象
    f.constructor = test;//增強(qiáng)對象
}

function Parent(name){
this.name = name;
this.arr = ['brother','sister','parents'];
}
Parent.prototype.run = function () {
return this.name;
};
function Child(name,age){
Parent.call(this,name);
this.age =age;
}
inheritPrototype(Parent,Child);//通過這里實(shí)現(xiàn)繼承

var test = new Child('trigkit4',21);
test.arr.push('nephew');
alert(test.arr);//
alert(test.run());//只共享了方法

var test2 = new Child('jack',22);
alert(test2.arr);//引用問題解決
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 開山例子 需求:學(xué)生交學(xué)費(fèi),中學(xué)生打8折;小學(xué)生打5折,然后通過打印的方法,顯示學(xué)生的名字、年齡記應(yīng)繳學(xué)費(fèi)。 我們...
    jayafs閱讀 597評論 0 50
  • 對于js繼承一直都是半懂半不懂的狀態(tài),感覺需要整理一下: 參考自:JavaScript原型繼承工作原理JavaSc...
    博客專用馬甲閱讀 4,295評論 2 20
  • 一、call()和apply()方法 1.方法定義 call方法: 語法:call([thisObj[,arg1[...
    O你的微笑好美O閱讀 433評論 0 0
  • 借用構(gòu)造函數(shù)繼承 原型鏈?zhǔn)嚼^承(借用原型鏈實(shí)現(xiàn)繼承) 組合式繼承 組合式繼承優(yōu)化1 組合式繼承優(yōu)化2 ES6中繼承...
    lxt410725閱讀 372評論 0 1
  • 我租房子的那個(gè)小區(qū)群的馬路頂頭喇叭口,是個(gè)水果鋪,水果品種豐富,鋪?zhàn)雍喴住?鋪?zhàn)邮且环N簡易的鋁合金玻璃房,但主要的...
    薇薇安的30天閱讀 333評論 1 0

友情鏈接更多精彩內(nèi)容