JavaScript 中 this 關鍵字的指向
1. 在方法 (method) 中
當 this 出現在物件的方法內部時,它指向的是調用該方法的物件。
const obj = {
name: "Coffee",
greet() {
console.log(this.name); // this 指向 obj
},
};
obj.greet(); // Coffee
2. 在普通函式 (function) 中
如果一個函式不是透過物件調用,而是直接執行,那麼 this 在非嚴格模式下 (non-strict mode) ...
jaoiddd.hashnode.dev1 min read