this的指向取决于函数调用方式,其规则按优先级分为:箭头函数继承外层作用域this;new绑定指向新实例;显式绑定(call/apply/bind)指定this值;隐式绑定指向调用对象;默认绑定指向全局或undefined。

JavaScript中的
this
this
要深入理解
this
this
最常见的几种绑定规则包括:
默认绑定 (Default Binding): 当函数作为独立函数被调用,没有明确的上下文对象时,
this
window
global
this
undefined
立即学习“Java免费学习笔记(深入)”;
function showThis() {
console.log(this);
}
showThis(); // 在浏览器中输出 Window 对象,在严格模式下输出 undefined隐式绑定 (Implicit Binding): 当函数作为对象的方法被调用时,
this
const person = {
name: 'Alice',
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
person.greet(); // 输出 "Hello, my name is Alice",因为 greet 是通过 person 对象调用的这里有个常见的陷阱:如果把
person.greet
const sayHello = person.greet; sayHello(); // 在非严格模式下输出 "Hello, my name is undefined" (或指向全局对象的 name),因为此时 greet 失去了 person 的上下文
显式绑定 (Explicit Binding): 我们可以强制改变
this
call()
apply()
bind()
call()
apply()
this
call()
apply()
bind()
this
bind()
function introduce(age, city) {
console.log(`I'm ${this.name}, ${age} years old, from ${city}.`);
}
const user = { name: 'Bob' };
introduce.call(user, 30, 'New York'); // I'm Bob, 30 years old, from New York.
introduce.apply(user, [25, 'London']); // I'm Bob, 25 years old, from London.
const boundIntroduce = introduce.bind(user, 40);
boundIntroduce('Paris'); // I'm Bob, 40 years old, from Paris. (注意这里 bind 也可以预设部分参数)new
new
this
function Car(make, model) {
this.make = make;
this.model = model;
}
const myCar = new Car('Honda', 'Civic');
console.log(myCar.make); // Honda
console.log(myCar.model); // Civic在这个场景下,
this
new
箭头函数 (Arrow Functions): 这是ES6引入的,它彻底改变了
this
this
this
this
const person = {
name: 'Charlie',
sayLater: function() {
setTimeout(function() {
// 这里的 this 默认指向全局对象 (Window),因为 setTimeout 的回调函数是独立调用的
console.log(`Regular function: ${this.name}`); // undefined 或全局对象的 name
}, 100);
},
sayLaterArrow: function() {
setTimeout(() => {
// 箭头函数捕获了 sayLaterArrow 定义时的 this,也就是 person 对象
console.log(`Arrow function: ${this.name}`); // Charlie
}, 100);
}
};
person.sayLater();
person.sayLaterArrow();箭头函数在处理回调函数时尤其方便,因为它避免了
this
bind
_this = this
我记得刚接触JavaScript时,
this
this
this
首先,多重绑定规则是主要原因。我们有默认绑定、隐式绑定、显式绑定、
new
new
this
其次,回调函数的上下文丢失是另一个常见的痛点。在异步操作(如
setTimeout
forEach
map
this
this
undefined
const self = this
.bind(this)
this
再者,严格模式的影响也增加了理解难度。在非严格模式下,默认绑定会指向全局对象,这有时会掩盖问题。但在严格模式下,默认绑定会是
undefined
对我来说,真正理解
this
this
判断
this
产品介绍微趣能 Weiqn 开源免费的微信公共账号接口系统。MVC框架框架结构清晰、易维护、模块化、扩展性好,性能稳定强大核心-梦有多大核心就有多大,轻松应对各种场景!微趣能系统 以关键字应答为中心 与内容素材库 文本 如图片 语音 视频和应用各类信息整体汇集并且与第三方应用完美结合,强大的前后台管理;人性化的界面设计。开放API接口-灵活多动的API,万名开发者召集中。Weiqn 系统开发者AP
1
是不是箭头函数?
this
this
是不是通过new
new MyConstructor()
this
是不是通过call()
apply()
bind()
myFunction.call(someObject, ...)
this
someObject
null
undefined
undefined
是不是作为对象的方法调用的?
myObject.myMethod()
this
myObject
.
默认绑定:
this
window
global
this
undefined
这个流程图能帮助我们覆盖绝大多数情况。举个例子:
const obj = {
name: 'Tester',
method: function() {
console.log(this.name);
},
arrowMethod: () => {
console.log(this.name); // 这里的 this 捕获的是 obj 定义时的全局 this,所以是 undefined 或 Window.name
}
};
obj.method(); // 步骤4:隐式绑定,this 指向 obj -> 'Tester'
const func = obj.method;
func(); // 步骤5:默认绑定,this 指向全局对象 -> undefined 或 Window.name
const anotherObj = { name: 'Another' };
obj.method.call(anotherObj); // 步骤3:显式绑定,this 指向 anotherObj -> 'Another'
obj.arrowMethod(); // 步骤1:箭头函数,this 捕获定义时的全局 this -> undefined 或 Window.name通过这种逐层判断,你可以更系统地分析
this
箭头函数是ES6带来的一项革命性特性,它在
this
this
this
this
这与传统的
function
this
this
this
核心改变:
解决了回调函数中的this
setTimeout
this
var self = this;
bind()
this
class Greeter {
constructor(name) {
this.name = name;
}
// 传统函数,this 在 setTimeout 回调中会丢失
greetOld() {
setTimeout(function() {
console.log(`Hello from old way, ${this.name}`); // this 指向 Window/undefined
}, 100);
}
// 箭头函数,this 捕获了 greetNew 方法定义时的 this (即 Greeter 实例)
greetNew() {
setTimeout(() => {
console.log(`Hello from new way, ${this.name}`); // this 指向 Greeter 实例
}, 100);
}
// 也可以直接将类方法定义为箭头函数,这样它的 this 总是指向实例
greetMethod = () => {
console.log(`Hello from arrow method, ${this.name}`);
}
}
const myGreeter = new Greeter('World');
myGreeter.greetOld(); // 输出 "Hello from old way, undefined"
myGreeter.greetNew(); // 输出 "Hello from new way, World"
myGreeter.greetMethod(); // 输出 "Hello from arrow method, World"
setTimeout(myGreeter.greetMethod, 200); // 即使作为回调,this 也保持不变更简洁的代码: 避免了冗余的
bind
self = this
不能作为构造函数: 箭头函数不能使用
new
this
this
没有arguments
arguments
arguments
不适用于定义对象方法: 如果你希望对象方法中的
this
this
const myObject = {
value: 42,
getValue: function() {
return this.value; // this 指向 myObject
},
getArrowValue: () => {
return this.value; // this 指向全局对象(Window/undefined),而非 myObject
}
};
console.log(myObject.getValue()); // 42
console.log(myObject.getArrowValue()); // undefined (或 Window.value)总的来说,箭头函数提供了一种更可预测、更稳定的
this
以上就是如何理解JavaScript中的this关键字?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号