- 1.任何情況下直接在script中寫入的this都是window。
- 2.函數中的this 非嚴格模式:this指向window, 嚴格模式時:this指向undefined。
- 3.箭頭函數的this this都指向箭頭函數外上下文環(huán)境的this指向
- 4.對象中this 對象屬性的this 指向對象外上下文環(huán)境的this 對象方法(普通函數)中的this,指向當前對象(誰執(zhí)行該方法,this就指向誰)
- 5.回調函數的this指向
- 1)、 setTimeout,setInterval回調函數不管是否是嚴格模式都會指向window。
- 2)、通過在函數內執(zhí)行當前回調函數 非嚴格模式:this指向window, 嚴格模式時:this指向undefined。
- 3)遞歸函數中的this 非嚴格模式:this指向window, 嚴格模式時:this指向undefined。 - 使用arguments0執(zhí)行函數時 this指向arguments。
- 5)事件中的回調函數,this指向事件偵聽的對象(e.currentTarget);
- 6、call,apply,bind方法執(zhí)行時this的指向 - 如果call,apply,bind傳參時,第一個參數傳入的不是null或者undefined,傳入什么this指向什么 - 如果第一個參數傳入的是null或者undefined ,非嚴格模式下指向window
- 7、在ES6的類中this的指向 - 構造函數中的this指向實例當前類所產生的新的實例對象 - 類中實例化方法中this指向誰執(zhí)行該方法,this指向誰 - 類中靜態(tài)方法中this執(zhí)行該類或者該類的構造函數 - 類中實例化箭頭方法,this仍然指向當前類實例化的實例對象
- 8、ES5的原型對象中this的指向 - 在原型的方法中,this指向實例化當前構造函數的實例化對象(誰執(zhí)行該方法,this指向誰); - 三種改變this指向的方式 - 函數名.call(this,....)this寫誰就指誰。 - 函數名.apply(this,[參數1,參數2,...]) this寫誰就指誰。 - 函數名. bind (this,1,2,3) this寫誰就指誰。