JavaScript中this指针指向问题

this指针


详情详见:阮一锋:Javascript 的 this 用法 http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html
this指针的指向:谁调用,指向谁。

   this.x = 9;
     var module ={
         x:81,
         getX:function () {
             console.log(this.x);
         }
     };
     console.log("第一个值:");
     module.getX();//81
     console.log("第二个值:");
     var retrieveX = module.getX;
     retrieveX();//9
     console.log("第三个值:");
     var boundGetX = retrieveX.bind(module);
     boundGetX();//81

执行结果:
第二个:module.getX赋值给retrieveX,this指针的指向改变成全局变量
第三个:retrieveX.bind(module);bind()函数绑定,改变this指向变成module

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章