目录
this指向
1、this
在每一个函数中,都有一个内置的变量this。大部分情况下,这个this存储的是当前函数的调用者。this的指向在函数定义的时候是确定不了的,只有在函数执行的时候才能确定this指向的是谁。
2.this的几种指向情况
1.在全局变量中,指向的是window。
function fn() {
console.log(this);
}
fn();
//this指向的是window
2.在对象的方法中指向的是调用者
var o = {
a: 10,
b: {
a: 12,
fn: function() {
console.log(this.a);//结果为12,当前this的调用者为b
}
}
}