this对象是在运行的时候基于函数的执行环境绑定(调用位置)的:在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象。
不过,匿名函数的执行环境具有全局性,因此其this对象通常指向window。
但有时候由于 编写闭包的方式不同,这一点可能不会那么明显。
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
//这里的this指向obj
console.log(this)
return function(){
//这里的this指向window
console.log(this)
return this.name;
};
}
}
alert(obj.getNameFunc()()); //"The Window" (在非严格模式下)
以上代码先创建了一个全局变量 name,又创建了一个包含 name 属性的对象。这个对象还包含一个方法——getNameFunc(),它返回一个匿名函数,而匿名函数又返回 this.name。为什么这个匿名函数没有取得其外部作用于的 this 对象呢?每个函数在被调用时,其活动对象都会自动取得两个特殊变量: this 和 arguments。内部函数在搜索这两个变量时,只会搜索到其活动对象为止,因此 永远不可能直接访问外部函数中的这两个变量。
var name = "leon"
var obj66 = {
name: "The Window",
obj: {
name: "My Obj",
// 方法
getNameFunc: function () {
//getNameFunc的直接调用者是obj
//这里的this指向obj
console.log(this)//obj
console.log(this.name);//My Obj
return function () {
//这里的this指向window
console.log(this)//window
console.log(this.name);//leon
return this.name;
};
}
}
}
alert(obj66.obj.getNameFunc()()); //"leon" (在非严格模式下)
var name = "leon"
var obj66 = {
name: "The Window",
obj: {
name: "My Obj",
// 方法
getNameFunc: function () {
//getNameFunc这里不是由obj调用,getNameFunc只是obj的一个属性,相当于取值。
//这里的this指向window
console.log(this)//window
console.log(this.name);//leon
return function () {
//这里的this指向obj,这里的return的function才是getNameFunc的真正方法。
console.log(this)//obj
console.log(this.name);//My Obj
return this.name;
};
}() //立即执行函数表达式形式。
}
}
alert(obj66.obj.getNameFunc()); //"My Obj" (在非严格模式下)
var name = "leon"
function foo() {
var name = "The Window";
var obj = {
name: "My Obj",
// 方法
getNameFunc: function () {
//这里的this指向obj
console.log(this)//obj
console.log(this.name);//My Obj
return function () {
//这里的this指向window
console.log(this)//window
console.log(this.name);//leon
return this.name;
};
}
}
alert(obj.getNameFunc()()); //"leon" (在非严格模式下)
}
foo(); //foo是在window下调用的,函数内部的this对象指向window
不过,把外部作用域中的this 对象保存在一个闭包能够访问到的变量里,技巧:var that = this;
,就可以让闭包访问该对象了,如下所示:
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
console.log(this.name); //"My Obj"
var that = this;//保存this
return function(){
return that.name;
};
}
}
alert(obj.getNameFunc()()); //"My Obj"
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
//这里的this指向window
console.log(this.name); //The Window
var that = this;//保存this
return function(){
return that.name;
};
}() // 立即执行函数表达式的形式
}
alert(obj.getNameFunc()); //The Window
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
//这里的this指向window
console.log(this.name); //The Window
//var that = this;//不保存this
return function(){
return this.name;
};
}() // 立即执行函数表达式的形式
}
alert(obj.getNameFunc()); //My Obj
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
//这里的this指向window
console.log(this.name); //The Window
//var that = this;//不保存this
return function(){
return this.name;
}(); // 立即执行函数表达式的形式
}() // 立即执行函数表达式的形式
}
alert(obj.getNameFunc); //The Window
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
//这里的this指向window
console.log(this.name); //My Obj
//var that = this;//不保存this
return function(){
return this.name;
}(); // 立即执行函数表达式的形式
}
}
alert(obj.getNameFunc()); //The Window
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
//这里的this指向window
console.log(this.name); //My Obj
var that = this;//不保存this
return function(){
return that.name;
}();
} // 立即执行函数表达式的形式
}
alert(obj.getNameFunc()); //My Obj
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
//这里的this指向window
console.log(this.name); //The Window
var that = this;//不保存this
return function(){
return that.name;
}();
}() // 立即执行函数表达式的形式
}
alert(obj.getNameFunc); //The Window
在几种特殊情况下, this 的值可能会意外地改变。比如,下面的代码是修改前面例子的结果:
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
return this.name;
}
}
console.log(obj.getNameFunc()); //"My Obj"
console.log((obj.getNameFunc)()); //"My Obj"
console.log((obj.getNameFunc = obj.getNameFunc)()); //"The Window" ,在非严格模式下
第三行代码先执行了一条赋值语句,然后再调用赋值后的结果。因为这个赋值表达式的值是函数本身,所以 this 的值不能得到维持(没有引用 obj,当前活动对象是 window),结果就返回了"The Window"。
var name = "The Window";
var obj = {
name : "My Obj",
// 方法
getNameFunc : function(){
return this.name;
}() //立即执行函数表达式的形式,this指向window
}
console.log(obj.getNameFunc); //"The Window"
console.log((obj.getNameFunc)); //"The Window"
console.log((obj.getNameFunc = obj.getNameFunc)); //"The Window" ,在非严格模式下
后续再补充。。。