谈谈This对象的理解
- 在全局作用域中 ,
this
指向全局对象(在浏览器环境中通常是window
对象)。 - 在函数中 ,
this
的值取决于函数的调用方式。- 如果函数是作为对象的方法调用,
this
指向调用该方法的对象。 - 如果函数是作为普通函数调用,
this
指向全局对象(非严格模式下)或undefined
(严格模式下)。 - 如果函数是通过
call
、apply
或bind
方法调用,this
指向call
、apply
或bind
方法的第一个参数所指定的对象。 - 如果函数是作为构造函数调用(使用
new
关键字),this
指向新创建的对象。
- 如果函数是作为对象的方法调用,
- 在箭头函数中 ,
this
的值是继承自外部作用域的,它不会因为调用方式的改变而改变。
下面是一些示例代码,以说明 this
的不同情况:
// 全局作用域中的 this
console.log(this); // 输出: Window
// 对象方法中的 this
const obj = {
name: 'poetry',
sayHello: function() {
console.log(`Hello, ${this.name}!`);
}
};
obj.sayHello(); // 输出: Hello, poetry!
// 普通函数调用中的 this
function greeting() {
console.log(`Hello, ${this.name}!`);
}
greeting(); // 输出: Hello, undefined (非严格模式下输出: Hello, [全局对象的某个属性值])
// 使用 call/apply/bind 改变 this
const person = {
name: 'poetry'
};
greeting.call(person); // 输出: Hello, poetry!
greeting.apply(person); // 输出: Hello, poetry!
const boundGreeting = greeting.bind(person);
boundGreeting(); // 输出: Hello, poetry!
// 构造函数中的 this
function Person(name) {
this.name = name;
}
const poetry = new Person('poetry');
console.log(poetry.name); // 输出: poetry
// 箭头函数中的 this
const arrowFunc = () => {
console.log(this);
};
arrowFunc(); // 输出: Window
THE END
暂无评论内容