直接上测试代码://直接调用
function outer(){
console.log(this); //this ==> window
(function inner(){
console.log(this); //this ==> window
})();
};
outer(); //函数返回undefined//通过构造函数调用
function outer(){
console.log(this); //this ==> outer
(function inner(){
console.log(this); //this ==> window
})();
};
new outer(); //构造函数返回outer//在obj对象下直接调用
function outer(){
console.log(this); //this ==> obj对象
(function inner(){
console.log(this); //this ==> window
})();
};
var obj = {};
obj.o = outer;
obj.o(); //函数返回undefined//在obj对象下通过构造函数调用
function outer(){
console.log(this); //this ==> outer
(function inner(){
console.log(this); //this ==> window
})();
};
var obj = {};
obj.o = outer;
new obj.o(); //构造函数返回outer