FWQ
JavaScript 中的 `this` 指向谁?
js中this的用法 如上所述,this的值根据函数的调用方式而变化,但始终表示调用函数的对象。那么,如何确定函数对应的对象呢?以下是一些常见的规则: 方法调用:当函数作为对象方法调用时,this表示该对象。例如: const obj = { name: "john", greet() { console.log(`hello, ${this.name}!`); } }; obj.greet(); // 输出 "hello, john!" 登录后复制 普通函数调用:当函数作为普通函数调用时,this指向全局对象(浏览器中为”window”,node.js中为”global”)。例如: function greet() { console.log(`hello, ${this.name}!`); }…