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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

單擊時(shí)調(diào)用回調(diào)函數(shù)未獲取當(dāng)前對(duì)象

單擊時(shí)調(diào)用回調(diào)函數(shù)未獲取當(dāng)前對(duì)象

蠱毒傳說 2022-12-18 16:03:32
我正在學(xué)習(xí) JS,但我有點(diǎn)無法理解發(fā)生了什么。我function在按下按鈕時(shí)打電話給 a,但 o/p 說是undefined。為什么我得到undefined?我想我沒有將對(duì)象作為一個(gè)整體傳遞,所以它不能從中引用但是當(dāng)我試圖在callBack添加事件監(jiān)聽器的功能之外打印它時(shí),我得到了正確的 O/P。const buttonM = document.getElementById("demo");let object = {  name: "Utkarsh",  surname: "sharma",  roll: 23,  objectFunction: function () {    console.log("Value is :" + this.name + " Surname:" + this.surname);  },};object.objectFunction(); //op: Value is: Utkarsh  Surname: Sharma (correct op as expected).buttonM.addEventListener("click", object.objectFunction);  //on pressing the button op:value is:  Surname:Undefined   (expected o/p is: Value is: Utkarsh  Surname: Sharma).注釋隨 op(輸出)一起提供
查看完整描述

3 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊

原因是因?yàn)閠his引用不同。


當(dāng)您最初調(diào)用該object.objectFunction();函數(shù)時(shí),您this就是對(duì)象本身,并且它具有name和的鍵surname。


當(dāng)您將object.objectFunction函數(shù)附加到按鈕的點(diǎn)擊偵聽器時(shí),將創(chuàng)建對(duì)該函數(shù)的引用,并且您將丟失其余object屬性。


希望一個(gè)例子可以解決這個(gè)問題:


const buttonM = document.getElementById("demo");


let object = {

  name: "Utkarsh",

  surname: "Sharma",


  objectFunction: function () {

    console.log("this    →", this); // ← I have added this line


    console.log("name    →", this.name)

    console.log("surname →", this.surname)

  },

};


object.objectFunction();

buttonM.addEventListener("click", object.objectFunction);

<button id="demo">click</button>


查看完整回答
反對(duì) 回復(fù) 2022-12-18
?
繁花不似錦

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊

當(dāng)您調(diào)用objectFunction時(shí)object,它會(huì)起作用,正如您已經(jīng)發(fā)現(xiàn)的那樣。但是,當(dāng)實(shí)際function被定義為事件處理程序時(shí),它不再綁定到對(duì)象。您可以創(chuàng)建一個(gè)調(diào)用它的函數(shù),例如


const buttonM = document.getElementById("demo");


let object = {

  name: "Utkarsh",

  surname: "sharma",

  roll: 23,

  objectFunction: function () {

    console.log(this);

    console.log("Value is :" + this.name + " Surname:" + this.surname);

  },

};


object.objectFunction(); //op: Value is: Utkarsh  Surname: Sharma (correct op as expected).


buttonM.addEventListener("click", () => {object.objectFunction()});  //on pressing the button op:value is:  Surname:Undefined   (expected o/p is: Value is: Utkarsh  Surname: Sharma).

請(qǐng)參閱:https ://jsfiddle.net/561so8e2/


或者您可以將對(duì)象綁定到點(diǎn)擊,如


const buttonM = document.getElementById("demo");


let object = {

  name: "Utkarsh",

  surname: "sharma",

  roll: 23,

  objectFunction: function () {

    console.log("Value is :" + this.name + " Surname:" + this.surname);

  },

};


object.objectFunction(); //op: Value is: Utkarsh  Surname: Sharma (correct op as expected).


buttonM.onclick = object.objectFunction;

buttonM.onclick.bind(object);

//buttonM.addEventListener("click", () => {object.objectFunction()});  //on pressing the button op:value is:  Surname:Undefined   (expected o/p is: Value is: Utkarsh  Surname: Sharma).

請(qǐng)參閱https://jsfiddle.net/561so8e2/2/


查看完整回答
反對(duì) 回復(fù) 2022-12-18
?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

只需將對(duì)象綁定到它應(yīng)該工作的回調(diào)


   const buttonM = document.getElementById("demo");

let object = {

  name: "Utkarsh",

  surname: "sharma",

  roll: 23,

  objectFunction: function () {

    let self = this;

    console.log("Value is :" + self.name + " Surname:" + self.surname);

  },

};


object.objectFunction(); //op: Value is: Utkarsh  Surname: Sharma (correct op as expected).

buttonM.addEventListener("click",object.objectFunction.bind(object)); 


查看完整回答
反對(duì) 回復(fù) 2022-12-18
  • 3 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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