JavaScript";这";关键字和闭包编译器警告
我有一个生产代码,我现在正试图用闭包编译器使用高级选项缩小它。代码使用。编译器会生成30多个以下警告:JavaScript";这";关键字和闭包编译器警告,javascript,scope,google-closure-compiler,Javascript,Scope,Google Closure Compiler,我有一个生产代码,我现在正试图用闭包编译器使用高级选项缩小它。代码使用。编译器会生成30多个以下警告: JSC_USED_GLOBAL_THIS: dangerous use of the global this object 以下是Paul库中的一些代码片段(oroginal代码使用命名函数,而不是匿名函数): 我读过,也读过。但是,我仍然不明白上述代码中的this的范围是什么,也不知道如何重写此代码以消除编译器警告 问题1:有人能为我澄清一下这个的范围是什么吗 问题2:是否有人可以提供一些
JSC_USED_GLOBAL_THIS: dangerous use of the global this object
以下是Paul库中的一些代码片段(oroginal代码使用命名函数,而不是匿名函数):
我读过,也读过。但是,我仍然不明白上述代码中的this
的范围是什么,也不知道如何重写此代码以消除编译器警告
问题1:有人能为我澄清一下这个
的范围是什么吗
问题2:是否有人可以提供一些代码片段,说明如何重写上述内容以消除编译器警告(我假设必须消除使用this
)
TIA.本的范围本质上是不可预测的
此
引用调用特定调用函数的任何上下文。例如:
var foo = function()//or function foo()
{
console.log(this);
};
foo();//logs the window object
var anObj = {name:'someObject',method:foo};
anObj.method();//logs `anObj`
基本原理很简单:
[[someObject]].function();//call func
/\ ||
|| implicit ||
|______________|
在第一个示例中,没有显式的所有者对象,因此默认情况下,JS返回到全局对象。(除非使用严格模式
)。第二个例子,foo被用作从对象调用的方法,因此这个引用该对象
现在,这一切都很简单,但是考虑到这个
引用是临时确定的,并且函数在JS中是松散绑定的(Array.prototype.slice.apply(arguments,[0]);
),无法保证它总是指向您期望它指向的内容
ECMAScript 5为此提供了bind
方法,使您能够将给定的上下文绑定到函数对象,但不要忘记:有些讨厌的人仍然使用过时的浏览器
此外,这是一个由来已久的“问题”,人们使用了各种解决方法。最简单的方法是使用闭包:
var MyConstructor = function()
{
"use strict";//in strict mode, this'll throw errors when called without new keyword
if (this === window || !this)
{
throw 'Omitted new keyword in older browsers, that don\'t support strict mode';
}
var that = this;//create closure reference to the new instance
that.someProperty = 'Use that instead of this';
that.especially = function()
{
console.log(this === that);//see later
console.log('especially in methods, because they can be borrowed, at which point `this` could reference anything, but here: ' + that + ' Will always be what you expect');
};
}
var foo = new MyConstructor();
foo.especially();//logs true, and 'especially in methods ...'
bar = [];
foo.especially.apply(bar,[]);//logs false, but the second log will still be the same
这个
一开始可能会让人感到困惑和棘手(以后当你认为自己得到了它时,仍然会让你措手不及)。但是再见,谢谢。仅供参考:您使用闭包的建议导致了以下问题:此外,编译器以意外的方式重写代码,这通常会更改this
关键字所引用的对象。请参阅@ChadKillingsworth:谢谢你的链接,我想这个名字说明了一切:如果你想让你的代码正常工作,尽量使用闭包:)。
[[someObject]].function();//call func
/\ ||
|| implicit ||
|______________|
var MyConstructor = function()
{
"use strict";//in strict mode, this'll throw errors when called without new keyword
if (this === window || !this)
{
throw 'Omitted new keyword in older browsers, that don\'t support strict mode';
}
var that = this;//create closure reference to the new instance
that.someProperty = 'Use that instead of this';
that.especially = function()
{
console.log(this === that);//see later
console.log('especially in methods, because they can be borrowed, at which point `this` could reference anything, but here: ' + that + ' Will always be what you expect');
};
}
var foo = new MyConstructor();
foo.especially();//logs true, and 'especially in methods ...'
bar = [];
foo.especially.apply(bar,[]);//logs false, but the second log will still be the same