JS函数(普通函数,箭头函数)中this的指向问题详解
本文详解JavaScript中普通函数与箭头函数的this指向规则。普通函数在非对象属性调用时指向全局对象,可通过bind/call/apply改变;作为对象方法时指向该对象。箭头函数this固定为定义时的上下文,bind/call/apply无法改变其指向,且不能作为构造函数使用。
普通函数
对于具名或匿名的普通函数,若其未作为对象的属性被调用,则函数内部的 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处理含Emoji或中文的字符串转数组乱码问题
2026-11-05 16:12:28
JS怎么使用展开运算符合并对象
2026-11-05 14:42:18
ES6+环境下判断JS对象为空的简洁写法
2026-11-05 12:12:23
怎么通过replacer函数自定义JS中JSON的格式化输出
2026-11-05 11:19:44
JS正则表达式基础:字面量与RegExp构造函数的区别及常用元字符
2026-11-05 11:16:58
