javascript
datagridview 当前上下文中不存在bind_全面解析JavaScript中this指向问题
this指向
參考文章:* this
JavaScript中this指向分為以下幾種情況:
- 普通函數或作為對象屬性
- 事件綁定
- 構造函數
- 箭頭函數
- call/apply/bind指定
下面我們來進行一一介紹
普通函數或作為對象屬性
this取決于方法執行前面是否有“點”,有“點”的話,“點”前面是誰this就是誰,如果沒有點的話,this指向window
const fn = function () {console.log(this); };const obj = { name: 'OBJ', fn };fn();obj.fn();const fn1 = obj.fn; fn1();answer:
1. window 2. {name: 'OBJ', fn: function() {console.log(this)}} // obj 3. window可以看到函數作為對象的屬性被調用的時候,其this指向調用該函數的對象,否則其this指向window
事件綁定
在進行事件綁定的時候,事件綁定函數中的this是綁定事件的元素:
// 假設頁面中有id為button的button元素 // var x = 100; window.x = 100; const fn = function () {console.log(this.x); }; const obj = { x: 200, fn }; const $button = document.getElementById('button'); $button.x = 300;obj.fn(); const fn1 = obj.fn; fn1();$button.addEventListener('click', fn); $button.addEventListener('mouseenter', obj.fn);$button.addEventListener('mouseleave', function () {obj.fn();});answer:
1. 200 2. 100 3. 點擊button時:300 4. 鼠標移入button時:300 5. 鼠標移出時:200但是需要注意的是,這里我們是在用戶點擊時,瀏覽器幫我們將點擊事件的this指向綁定該事件的DOM元素。如果通過代碼來觸發對應的事件的話,我們可以通過call/apply/bind來指定其this
$button.click.call() // this為window,打印結果為100構造函數(new Fn)
構造函數(new Fn)執行,函數中的this是當前類的實例,這是new關鍵字幫我們做到的:
var x = 100; const Fn = function () {this.x = 200;console.log(this.x); };const fn = new Fn();answer:
1. 200箭頭函數
箭頭函數中沒有自身的this,所用到的this都是其最近父級上下文中的this
const fn = function () {console.log(this);setTimeout(() => {console.log(this);}, 1000);setTimeout(function () {console.log(this);}); };const obj = { x: 100, fn };obj.fn();answer:
1. {x:100, fn: function() {...}} // obj 2. window 3. {x:100, fn: function() {...}} // objcall/apply/bind改變this指向
為call/apply/bind傳入的第一個參數即為函數的this:
var x = 100; const obj = { x: 200, y: 200 }; const fn = function () {console.log(this.x); };fn(); fn.call(obj); fn.apply(obj);const fixedThisFn = fn.bind(obj); fixedThisFn();answer:
1. 100 2. 200 3. 200 4. 200- call在執行時,第一個參數為this指向,之后的參數為fn執行時的參數
- apply在執行時,第一個參數為this指向,之后的參數為fn執行時的參數組成的數組,數組的每一項會和fn的每一個參數進行對應
- bind在執行時,第一個參數為預先傳入this指向,之后的參數為實際調用fn前預先傳入的參數,返回值為一個函數fixedThisFn,fixedThisFn內部會調用fn并指定其this指向
為了更深入的理解call/apply/bind是如何改變函數中this指向的,下面我們分別模擬實現這三個函數
call/apply/bind源碼實現
根據前面的介紹,我們知道:當函數作為對象屬性被調用時,this指向調用該函數的對象
const obj = { x: 100, fn () {console.log(this);} }; obj.fn(); // {x: 100, fn: function() {...}} => obj利用JavaScript這個特性,我們可以將執行的函數作為call/apply的第一個參數context的屬性,然后通過context來調用該屬性對應的函數,函數的this便指向了context
call的源碼模擬如下:
Function.prototype.myOwnCall = function (context, ...args) {const uniqueKey = new Date().getTime();// this為調用call方法的函數context[uniqueKey] = this;// 作為對象的方法被對象調用,this指向該對象contextconst result = context[uniqueKey](...args);delete context[uniqueKey];return result; };到這里,有的小伙伴可能已經發現了,如果call/apply傳入的context不是對象呢?
首先我們看下mdn對call方法的第一個參數的描述:
語法:function.call(thisArg, arg1, arg2, ...)* thisArg
可選的。在function函數運行時使用的this值。請注意,this可能不是該方法看到的實際值:如果這個函數處于非嚴格模式下,則指定null或undefined時會自動替換為指向全局對象,原始值會被包裝
接下來,我們對myOwnCall方法的第一個參數做如下處理:
function translateToObject (context) {// 可以通過 == 進行判斷 context == null// null == undefined => 2個等號是成立的// null,undefined => windowif (typeof context === 'undefined' || context === null) {context = window;} else if (typeof context === 'number') { // 原始值轉換為包裝對象context = new Number(context);} else if (typeof context === 'string') {context = new String(context);} else if (typeof context === 'boolean') {context = new Boolean(context);}return context; }在myOwnCall方法中調用該函數:
Function.prototype.myOwnCall = function (context, ...args) {context = translateToObject(context);const uniqueKey = new Date().getTime();// this為調用call方法的函數context[uniqueKey] = this;// 作為對象的方法被對象調用,this指向該對象contextconst result = context[uniqueKey](...args);delete context[uniqueKey];return result; };apply的實現與call基本相同,只不過第二個參數是一個數組:
Function.prototype.myOwnBind = function (context, paramsArray) {context = translateToObject(context);const uniqueKey = new Date().getTime();// this為調用call方法的函數context[uniqueKey] = this;// 作為對象的方法被對象調用,this指向該對象contextconst result = context[uniqueKey](...paramsArray);delete context[uniqueKey];return result; };相比于call/apply,bind函數并沒有立即執行函數,而是預先傳入函數執行時的this和參數,并且返回一個函數,在返回的函數中執行調用bind函數并將預先傳入的this和參數傳入
bind的源碼模擬:
Function.prototype.myOwnBind = function (context, ...outerArgs) {const fn = this;return function (...innerArgs) {return fn.call(context, ...outerArgs, ...innerArgs);}; };精簡版如下:
Function.prototype.myOwnBind = (context, ...outerArgs) => (...innerArgs) => this.call(context, ...outerArgs, ...innerArgs); 這里并沒有實現通過new操作符來執行fn.bind(context)的操作,如果想知道其詳細的實現過程,可以看我的這篇文章: JS進階-手寫bind在深入理解call/apply/bind的實現原理后,我們嘗試完成下面的測試:
function fn1 () {console.log(1);} function fn2 () {console.log(2);} fn1.call(fn2);fn1.call.call(fn2);Function.prototype.call(fn1); Function.prototype.call.call(fn1);answer:
1. 1 2. 2 3. 什么都不輸出 4. 1這里我們根據call的源碼來進行推導一下Function.prototype.call.call(fn1),其它的執行過程類似:
// 1. 首先會將Function.prototype.call作為一個函數來執行它原型上的call方法 // 所以call方法內部: // this => Function.prototype.call // context => fn1 // 通過對象的屬性來執行方法改變this指向 // fn1[uniqueKey] = this(Function.prototype.call) // fn1[uniqueKey]() // 執行 Function.prototype.call方法,但是this是context // 2. 在this為fn1的情況下執行Function.prototype.call方法 // 所以call方法內部: // this => fn1 // context => window // 通過對象的屬性來改變this指向 // window[uniqueKey] = fn1 // window[uniqueKey]() // 執行fn1(),但是this是window這里就是有筆者關于JavaScript中this指向的相關內容的理解,希望能對閱讀的小伙伴有所幫助
總結
以上是生活随笔為你收集整理的datagridview 当前上下文中不存在bind_全面解析JavaScript中this指向问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 广发信用卡积分换里程怎么换
- 下一篇: 第三方理财产品和P2P的区别,看完就懂了