this。

题目

var myObject = {
    foo: "bar",
    func: function() {
        var self = this;
        console.log("outer func:  this.foo = " + this.foo);
        console.log("outer func:  self.foo = " + self.foo);
        (function() {
            console.log("inner func:  this.foo = " + this.foo);
            console.log("inner func:  self.foo = " + self.foo);
        }());
    }
};
myObject.func();

答案

outer func:  this.foo = bar 
outer func:  self.foo = bar 
inner func:  this.foo = undefined 
inner func:  self.foo = bar

解析

这道题的易错点在最后一个 console:最后一个 console 虽然也在立即执行表达式中,但是在这里 self 是从外部拿到的一个变量,只是不这个变量指向this罢了。