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