js函数( 普通函数、箭头函数 ) 内部this的指向
- 普通函数
| 具名普通函数、匿名普通函数,在不作为对象的属性值的情况下,其内部的 this 总是指向代码运行环境下的全局对象 ( 例如,浏览器中的 window )。
示例:
1 2 3 4 5 6 7 8 9 | ( function () { console.log( this ); // window ( function () { console.log( this ); // window ( function () { console.log( this ); // window })() })() })() |
| 普通函数,均可以通过其 bind、call、apply 方法 来改变其内部 this 的指向。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 | ( 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 指向该属性所直接归属的对象 。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ( 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。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | ( 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 的指向,虽不会报错但却是无效的。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ( 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) // 1 console.log(b) // 2 console.log(c) // 3 }).bind( null , 1, 2)(3) }).bind( 'hello' )() |
| 附:
* 箭头函数不能作为构造函数使用,强制使用 new 运算符作用在箭头函数上,将会报如下错误
1 | new (() => {}) // Uncaught TypeError: (intermediate value) is not a constructor |
* 箭头函数内部没有定义 arguments 变量,箭头函数所在的作用域也不存在 arguments 的情况下,应用该变量会报错。
1 2 3 4 5 6 7 8 9 10 | ( function () { ((a) => { console.log(a) // 1 console.log(arguments) // Arguments ['hello'] })(1) })( 'hello' ); (() => { console.log(arguments) // Uncaught ReferenceError: arguments is not defined })(); |
* 普通函数都有原型属性 prototype,箭头函数没有这个属性。
1 2 | ( function () {}).prototype // {constructor: ƒ} (() => {}).prototype // undefined |
分类:
javascript
标签:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端