亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于構造函數的問題

關于構造函數的問題

素胚勾勒不出你 2019-03-12 13:15:16
function Parent() {}Parent.prototype.func1 = function(callback) {    this.callback = callback;}Parent.prototype.func2 = function() {    this.callback.fetchData()}在構造函數中,是不是func1定義的屬性,如this.callback。在func2中也可以訪問?
查看完整描述

3 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

是的,原型和構造函數中的this指向的都是將來要創建的實例


查看完整回答
反對 回復 2019-03-26
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

實例化后,使用獲得的對象執行一次func1,該對象中調用func2時就可以訪問thi.callback了


查看完整回答
反對 回復 2019-03-26
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

可以 構造函數中的this是實例對象,這些屬性是掛在實例對象上的。


function Parent() {


}


Parent.prototype.func1 = function(callback) {

    this.callback = callback;

}

Parent.prototype.func2 = function() {

  console.log(this.callback)

    this.callback.fetchData()

}




Parent.prototype.func1({fetchData: function(){

    console.log(2)

}})


let per = new Parent()

per.func2()

https://img1.sycdn.imooc.com//5c9989020001df5906000192.jpg

查看完整回答
反對 回復 2019-03-26
  • 3 回答
  • 0 關注
  • 501 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號