我正在学习 JS,但我有点无法理解发生了什么。
我function在按下按钮时打电话给 a,但 o/p 说是undefined。为什么我得到undefined?我想我没有将对象作为一个整体传递,所以它不能从中引用但是当我试图在callBack添加事件监听器的功能之外打印它时,我得到了正确的 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(输出)一起提供
慕娘9325324
繁花不似锦
缥缈止盈
相关分类