网站首页 > 技术文章 正文
在 JavaScript 中,this的指向是一个核心概念,其值取决于函数的调用方式,而非定义位置(箭头函数除外)。以下是this指向的常见场景及具体说明:
1. 全局作用域中的this
在全局作用域(非函数内部),this指向全局对象:
- 浏览器环境中,全局对象是window;
- Node.js 环境中,全局对象是global。
console.log(this === window); // 浏览器中:true
console.log(this === global); // Node.js中:true
注意:严格模式("use strict")下,全局作用域的this仍然指向全局对象(与非严格模式一致)。
2. 普通函数调用(独立调用)
当函数独立调用(不依附于任何对象)时,this的指向分两种情况:
- 非严格模式:this指向全局对象(浏览器中为window);
- 严格模式:this为undefined。
// 非严格模式
function foo() {
console.log(this); // window(浏览器)
}
foo(); // 独立调用
// 严格模式
function bar() {
"use strict";
console.log(this); // undefined
}
bar(); // 独立调用
3. 对象方法调用
当函数作为对象的方法被调用时,this指向调用该方法的对象。
const obj = {
name: "Alice",
sayHi: function() {
console.log(this.name); // this指向obj
}
};
obj.sayHi(); // 输出:"Alice"(调用者是obj)
特殊情况:如果方法被赋值给变量后独立调用,this会变回全局对象(非严格模式)或undefined(严格模式):
const say = obj.sayHi;
say(); // 非严格模式下输出:undefined(this指向window,window.name为空)
4. 构造函数调用(new关键字)
当函数通过new关键字作为构造函数调用时,this指向新创建的实例对象。
function Person(name) {
this.name = name; // this指向新实例
}
const p = new Person("Bob");
console.log(p.name); // 输出:"Bob"(this指向p)
注意:如果忘记使用new,函数会变为普通调用,this指向全局对象:
const p = Person("Bob"); // 错误:未使用new
console.log(window.name); // 输出:"Bob"(this指向window)
5. 箭头函数中的this
箭头函数没有自己的this,其this继承自外层作用域的this(定义时确定,永不改变)。
const obj = {
foo: function() {
// 普通函数,this指向obj
const bar = () => {
console.log(this); // 箭头函数继承foo的this(即obj)
};
bar();
}
};
obj.foo(); // 输出:obj
场景对比:箭头函数 vs 普通函数
const timer = {
delay: 100,
start: function() {
// 普通函数:this指向window(非严格模式)
setTimeout(function() {
console.log(this.delay); // 输出:undefined(window.delay不存在)
}, this.delay);
// 箭头函数:this继承start的this(即timer)
setTimeout(() => {
console.log(this.delay); // 输出:100(timer.delay)
}, this.delay);
}
};
timer.start();
6. 事件处理函数中的this
在 DOM 事件处理函数中,this通常指向触发事件的元素(即绑定事件的 DOM 节点)。
<button id="btn">点击我</button>
<script>
const btn = document.getElementById("btn");
btn.onclick = function() {
console.log(this); // 指向按钮元素(<button>)
console.log(this === btn); // true
};
</script>
注意:如果用箭头函数作为事件处理函数,this会继承外层作用域的this(通常是window):
btn.onclick = () => {
console.log(this); // 指向window(外层作用域的this)
};
7.apply/call/bind改变this指向
这三个方法可以手动指定函数中this的指向:
- func.call(thisArg, arg1, arg2, ...):立即调用函数,参数逐个传入;
- func.apply(thisArg, [argsArray]):立即调用函数,参数以数组形式传入;
- func.bind(thisArg, arg1, arg2, ...):返回新函数,this被永久绑定,参数可部分预设(柯里化)。
function greet() {
console.log(`Hello, ${this.name}`);
}
const person1 = { name: "Charlie" };
const person2 = { name: "Diana" };
greet.call(person1); // 输出:"Hello, Charlie"(this指向person1)
greet.apply(person2); // 输出:"Hello, Diana"(this指向person2)
const greetCharlie = greet.bind(person1);
greetCharlie(); // 输出:"Hello, Charlie"(this永久绑定person1)
总结
this的指向遵循以下核心规则:
- 普通函数:由调用方式决定(独立调用→全局 /undefined;对象调用→该对象);
- 构造函数:new关键字使this指向新实例;
- 箭头函数:无自身this,继承外层作用域的this;
- 特殊方法:apply/call/bind可手动指定this。
理解this的关键是明确函数的调用场景,不同的调用方式会导致this指向不同的对象。
猜你喜欢
- 2025-09-21 Google 开源 zx,用 async/await 编写 shell 脚本
- 2025-09-21 macOS 上如何写自定义命令行工具?
- 2025-09-21 为什么需要前端水印?_前端水印和后端水印的差别
- 2025-09-21 说说 React 中 fiber、DOM、ReactElement、实例对象之间的引用关系
- 2025-09-21 前端必会:Promise 全解析,从原理到实战
- 2024-12-13 一文让你彻底搞懂 vuex,满满的干货
- 2024-12-13 vue前端通过Base64编码解码
- 2024-12-13 「前端进阶」高性能渲染十万条数据(虚拟列表)
- 2024-12-13 Web前端新人笔记之了解Jquery
- 2024-12-13 typeScript 学习笔记(二)——数据类型大全
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- oracle面试 (55)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)