JS函数(普通函数,箭头函数)中this的指向问题详解

Serepta ·
更新时间:2024-09-20
· 1226 次阅读

目录

普通函数

箭头函数

普通函数

具名普通函数、匿名普通函数,在不作为对象的属性值的情况下,其内部的 this 总是指向代码运行环境下的全局对象 ( 例如,浏览器中的 window )。

示例:

(function() { console.log(this); // window (function() { console.log(this); // window (function() { console.log(this); // window })() })() })()  

普通函数,均可以通过其 bind、call、apply 方法 来改变其内部 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'} })() 

当普通函数( 具名的、匿名的、外部定义的方法 ),作为对象的属性值被引用的时候,其内部的 this 指向该属性所直接归属的对象 。

示例:

(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 })()  箭头函数

箭头函数,不管是作为独立的方法 或是 作为对象的属性值,其内部的 this 均指向 该箭头函数被定义时所在的上下文中对应的 this。

示例:

(function() { /** 外层作用域 */ const arrowfunc = () => console.log(this) console.log('-- 外层作用域 --'); console.log(this); // String {'hello'} arrowfunc(); // String {'hello'} (function() { /** 内层作用域 */ const arrowfunc1 = () => console.log(this) console.log('-- 内层作用域 --'); console.log(this); // String {'world'} arrowfunc() // String {'hello'} arrowfunc1() // String {'world'} /** 函数作为对象属性值 */ const obj = { arrowfunc, arrowfunc1, param: { arrowfunc, arrowfunc1, arrowfunc2: () => console.log(this) } } console.log('-- 函数作为对象属性值 --'); 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')()

箭头函数 也有 bind、call、apply 方法,与普通函数一样可以通过这三个方法预设箭头函数的入参值。

试图通过这三个方法改变箭头函数内部 this 的指向,虽不会报错但却是无效的。

示例:

(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')()  

附:

* 箭头函数不能作为构造函数使用,强制使用 new 运算符作用在箭头函数上,将会报如下错误

new (() => {}) // Uncaught TypeError: (intermediate value) is not a constructor  

* 箭头函数内部没有定义 arguments 变量,箭头函数所在的作用域也不存在 arguments 的情况下,应用该变量会报错。

(function() { ((a) => { console.log(a) // 1 console.log(arguments) // Arguments ['hello'] })(1) })('hello'); (() => { console.log(arguments) // Uncaught ReferenceError: arguments is not defined })();

* 普通函数都有原型属性 prototype,箭头函数没有这个属性。

(function() {}).prototype // {constructor: ƒ} (() => {}).prototype // undefined

到此这篇关于JS函数(普通函数,箭头函数)中this的指向问题详解的文章就介绍到这了,更多相关JS函数this的指向内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



js函数 this js

需要 登录 后方可回复, 如果你还没有账号请 注册新账号