Anm*_*raf 3 javascript scope this anonymous-function npm
我从grunt npm模块的源代码中看到以下代码行 -
String.prototype.__defineGetter__(method, function() { return this; });
Run Code Online (Sandbox Code Playgroud)
只是想在上面的匿名函数中预测"this"的价值 -
谢谢您的帮助 !!
this
将是get操作发生的对象,我认为它将永远是你调用的对象__defineGetter__
(因为我无法立即看到将该函数转移到其他地方的方法,但我不保证你不能;但你必须故意这样做.
值得注意的是,这__defineGetter__
是非标准和过时的.目前定义getter的方法是使用Object.defineProperty
or Object.defineProperties
,如下所示:
Object.defineProperty(String.prototype, "foo", {
get: function() {
// Here, `this` is the string
return this.toUpperCase();
}
});
console.log("hi there".foo);
Run Code Online (Sandbox Code Playgroud)
...哪些日志"HI THERE"
.
归档时间: |
|
查看次数: |
115 次 |
最近记录: |