闭包 this,arguemnts 问题

因为每个函数在被调用时,其活动对象都会自动取得两个特殊的变量,this和arguments.内部函数的搜索这两个变量时,只会搜索到其活动对象为止。因此永远不可能直接访问到外部函数中的这两个变量
    var name = "the windows";
    var obj = {
                        name:"lin615",
                        getName: function(){
                                return function(){
                                    return this.name;    
                                };
                        }
                    };

    // alert(obj.getName()()); // the windows

    //=========================================

    // alert(obj.getName());  结果为
    /*
    function (){
                                    return this.name;    
                                }
    */

=================================
如果把外部作用域中的 this 对象保存在一个闭包能够访问到的变量里,就可以让闭包访问该对象了。

    var name = "the windows";
    var obj = {
            name:"lin3615",
      getName: function(){
                var that = this;
                return function(){
                    return (that.name);
                };
            }
        };

        alert(obj.getName()()); //  lin3615
此时把 this 对象赋值给一个名叫 that 的变量,而在定义了闭包之后,闭包也可以访问这个变量。因为它是我们在包含函数中特意声名的一个变量。即使在函数返回之后,that 也仍然引用 obj。其实 this和arguments 一样的

原文地址:https://www.cnblogs.com/lin3615/p/3646960.html