跳到正文
当下的七炎
返回

匿名函数和this的理解

编辑文章

mahua 运行结果 mahua

     var name = "The Window";
      var object = {
        name : "My Object",
        getNameFunc : function(){
        
          return function(){
            return this.name;
          };
        }
      };
    //object.getNameFunc()()因为匿名函数的this指向window(没有其他的干扰)
    console.log("object.getNameFunc()()="+object.getNameFunc()());
    //普通函数的对象是windows,其实这里的test(),相当于window.test()
    var test=object.getNameFunc();
    console.log("test()="+test());
    //这个this指向的是object,object.name就是My Object了
    object.other=object.getNameFunc();
    console.log("object.other()="+object.other());
    //下面这个this指向是o。o.name没有值,也没有name定义所以是undefined
    var o={};
    o.other=object.getNameFunc();
    console.log("o.other()="+o.other());

编辑文章
分享这篇文章:

评论

使用 GitHub 账号登录后即可评论


上一篇
.gitignore
下一篇
闭包