第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Javascript中的apply和call繼承

標(biāo)簽:
Html/CSS Html5 JavaScript

call实现继承

call这里call的意思就是把animal的方法应用到cat这个对象身上,也就是animal的属性创建到了cat里面,所以cat就继承了animal的方法

function animal(a, b) {
    this.type = 'animal'
    this.behavior = function () {
        console.log(this.type + " is running")
    }
}

function cat(a, b) {
    this.name = 'wsscat'
    //这里call的意思就是把animal的方法应用到cat这个对象身上
    //所以cat就继承了animal的方法
    animal.call(this);
}

console.log(new cat())

call实现多重继承

当然我们可以继承多个构造函数,这就是多重继承

function animal(a, b) {
    this.type = 'animal'
    this.behavior = function () {
        console.log(this.type + " is running")
    }
}

function wsscat(a, b) {
    this.age = 0
}

function cat(a, b) {
    this.name = 'wsscat'
    //这里call的意思就是把animal的方法应用到cat这个对象身上
    //所以cat就继承了animal的方法
    animal.call(this);
    wsscat.call(this);
}

console.log(new cat())

只要在cat的构造函数中有多个call就可以,此时的cat继承了wsscat和animal

apply和call的区别

其实apply和call这两个方法基本上是差不多的,区别在于call的第二个参数可以是任意类型,而apply的第二个参数必须是数组,也可以是arguments(即传给构造函数的参数)

例如我们把上面的代码稍微改一下,如果此时我在new构造函数cat的时候传入参数new cat('wsscat','cute')我们的cat能接收arguments,但是如果此时继承是animal.call(this),没有给call传第二个参数的时候,生成的对象中type的值就会是undefined,所以为了让这个值能够让animal接收,我们可以在animal中传入第二个参数animal.call(this,type)

function animal(type) {
    this.type = type
    this.behavior = function () {
        console.log(this.type + " is running")
    }
}

function cat(name, type) {
    this.name = name
    //这里call的意思就是把animal的方法应用到cat这个对象身上
    //所以cat就继承了animal的方法
    //animal.call(this);//type undefined
    //animal.call(this,type);//type cute
    //animal.call(this,arguments[1]);//type cute
    //animal.call(this,arguments);//type ['wsscat','cute']
    animal.apply(this, arguments) //type: wsscat
}

console.log(new cat('wsscat', 'cute'))

这里用apply就很方便,因为arguments是数组,可以全部传给animal,而call就要一个个地传过去

  • animal.call(this);//type undefined
  • animal.call(this,type);//type cute
  • animal.call(this,arguments[1]);//type cute
  • animal.call(this,arguments);//type [‘wsscat’,‘cute’]
  • animal.apply(this,arguments)//type: wsscat

继承的优化

如果构造函数this绑定太多属性(比如一些共同方法),在实例化后会造成浪费,为此我们一般会使用原型链来优化,但是使用原型链之后我们的apply和call的继承方法就会失效
为此我们一般使用混合的写法,使用原型链和(apply或者call)方法进行继承
具体两句话
让子的原型链指向父的实例(父实例化的对象)
cat.prototype = new animal();
让父的属性创建在子的this上
animal.call(this, type)
整体代码如下,那么就会让父原型链的属性和this上的属性都得到继承

function animal(type) {
    this.type = type
    this.behavior = function () {
        console.log(this.type + " is running")
    }
}
animal.prototype.action = function () {
    console.log("running")
}

function cat(name, type) {
    this.name = name
    animal.call(this, type)
}

cat.prototype = new animal();
console.log(new cat('wsscat', 'cute'));
(new cat('wsscat')).action() //running
點(diǎn)擊查看更多內(nèi)容
3人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消