JS函數(shù)(普通函數(shù),箭頭函數(shù))中this的指向問(wèn)題詳解
普通函數(shù)
具名普通函數(shù)、匿名普通函數(shù),在不作為對(duì)象的屬性值的情況下,其內(nèi)部的 this 總是指向代碼運(yùn)行環(huán)境下的全局對(duì)象 ( 例如,瀏覽器中的 window )。
示例:
(function() { console.log(this); // window (function() { console.log(this); // window (function() { console.log(this); // window })() })() })()
普通函數(shù),均可以通過(guò)其 bind、call、apply 方法 來(lái)改變其內(nèi)部 this 的指向。
示例:
(function() { const func = (function() { console.log(this) }).bind('hello') const obj = { func, func1: (function() { console.log(this) }).bind('hello'), func2: (function F() { console.log(this) }).bind('hello') } func() // String {'hello'} obj.func() // String {'hello'} obj.func1() // String {'hello'} obj.func2() // String {'hello'} })()
當(dāng)普通函數(shù)( 具名的、匿名的、外部定義的方法 ),作為對(duì)象的屬性值被引用的時(shí)候,其內(nèi)部的 this 指向該屬性所直接歸屬的對(duì)象 。
示例:
(function() { const func = function() { console.log(this) } const obj = { func, func1: function F() { console.log(this) }, func2() { console.log(this) }, param: { func, func1: function F() { console.log(this) }, func2() { console.log(this) } } } func() // window obj.func() // obj obj.func1() // obj obj.func2() // obj obj.param.func() // obj.param obj.param.func1() // obj.param obj.param.func2() // obj.param })()
箭頭函數(shù)
箭頭函數(shù),不管是作為獨(dú)立的方法 或是 作為對(duì)象的屬性值,其內(nèi)部的 this 均指向 該箭頭函數(shù)被定義時(shí)所在的上下文中對(duì)應(yīng)的 this。
示例:
(function() { /** 外層作用域 */ const arrowfunc = () => console.log(this) console.log('-- 外層作用域 --'); console.log(this); // String {'hello'} arrowfunc(); // String {'hello'} (function() { /** 內(nèi)層作用域 */ const arrowfunc1 = () => console.log(this) console.log('-- 內(nèi)層作用域 --'); console.log(this); // String {'world'} arrowfunc() // String {'hello'} arrowfunc1() // String {'world'} /** 函數(shù)作為對(duì)象屬性值 */ const obj = { arrowfunc, arrowfunc1, param: { arrowfunc, arrowfunc1, arrowfunc2: () => console.log(this) } } console.log('-- 函數(shù)作為對(duì)象屬性值 --'); obj.arrowfunc() // String {'hello'} obj.arrowfunc1() // String {'world'} obj.param.arrowfunc() // String {'hello'} obj.param.arrowfunc1() // String {'world'} obj.param.arrowfunc2() // String {'world'} }).bind('world')() }).bind('hello')()
箭頭函數(shù) 也有 bind、call、apply 方法,與普通函數(shù)一樣可以通過(guò)這三個(gè)方法預(yù)設(shè)箭頭函數(shù)的入?yún)⒅怠?/p>
試圖通過(guò)這三個(gè)方法改變箭頭函數(shù)內(nèi)部 this 的指向,雖不會(huì)報(bào)錯(cuò)但卻是無(wú)效的。
示例:
(function() { console.log(this); // String {'hello'} (() => { console.log(this); // String {'hello'} (() => { console.log(this) // String {'hello'} }).bind('bbb')() }).bind('aaa')(); ((a, b, c) => { console.log(this) // String {'hello'} console.log(a) // a console.log(b) // b console.log(c) // c }).bind(null, 1, 2)(3) }).bind('hello')()
附:
* 箭頭函數(shù)不能作為構(gòu)造函數(shù)使用,強(qiáng)制使用 new 運(yùn)算符作用在箭頭函數(shù)上,將會(huì)報(bào)如下錯(cuò)誤
new (() => {}) // Uncaught TypeError: (intermediate value) is not a constructor
* 箭頭函數(shù)內(nèi)部沒有定義 arguments 變量,箭頭函數(shù)所在的作用域也不存在 arguments 的情況下,應(yīng)用該變量會(huì)報(bào)錯(cuò)。
(function() { ((a) => { console.log(a) // 1 console.log(arguments) // Arguments ['hello'] })(1) })('hello'); (() => { console.log(arguments) // Uncaught ReferenceError: arguments is not defined })();
* 普通函數(shù)都有原型屬性 prototype,箭頭函數(shù)沒有這個(gè)屬性。
(function() {}).prototype // {constructor: ?} (() => {}).prototype // undefined
到此這篇關(guān)于JS函數(shù)(普通函數(shù),箭頭函數(shù))中this的指向問(wèn)題詳解的文章就介紹到這了,更多相關(guān)JS函數(shù)this的指向內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js中的bigint類型轉(zhuǎn)化為json字符串時(shí)報(bào)無(wú)法序列化的問(wèn)題
JSON序列化指將JSON對(duì)象轉(zhuǎn)換為JSON字符串,J實(shí)現(xiàn)方式有兩種:一種是調(diào)用JSON對(duì)象內(nèi)置的stringify()函數(shù),一種是為對(duì)象自定義toJSON()函數(shù),本文重點(diǎn)介紹js中的bigint類型轉(zhuǎn)化為json字符串時(shí)報(bào)無(wú)法序列化的問(wèn)題,感興趣的朋友一起看看吧2024-01-01淺談JS對(duì)html標(biāo)簽的屬性的干預(yù)以及對(duì)CSS樣式表屬性的干預(yù)
下面小編就為大家?guī)?lái)一篇淺談JS對(duì)html標(biāo)簽的屬性的干預(yù)以及對(duì)CSS樣式表屬性的干預(yù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06JS this關(guān)鍵字在ajax中使用出現(xiàn)問(wèn)題解決方案
這篇文章主要介紹了JS this關(guān)鍵字在ajax中使用出現(xiàn)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07CocosCreator通用框架設(shè)計(jì)之網(wǎng)絡(luò)
這篇文章主要介紹了CocosCreator通用框架設(shè)計(jì)之網(wǎng)絡(luò),詳細(xì)講解了WebSocket的原理和使用方法,對(duì)WebSocket感興趣的同學(xué),一定要看一下2021-04-04詳解JavaScript什么情況下不建議使用箭頭函數(shù)
箭頭函數(shù)作為ES6新增的語(yǔ)法,在使用時(shí)不僅能使得代碼更加簡(jiǎn)潔,而且在某些場(chǎng)景避免this指向問(wèn)題。但是箭頭函數(shù)不是萬(wàn)能的,也有自己的缺點(diǎn)以及不適用的場(chǎng)景,本文總結(jié)了JavaScript什么情況下不建議使用箭頭函數(shù),感興趣的可以了解一下2022-06-06