JavaScript中this指针指向问题

xiaoxiao2025-11-07  5

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。

转载请注明原文地址: https://www.6miu.com/read-5039221.html

最新回复(0)