admin管理员组文章数量:1327665
I have some code that looks like this:
var MyObject = function () {
this.Prop1 = "";
this.Prop2 = [];
this.Prop3 = {};
this.Prop4 = 0;
}
And then I later have this:
var SomeObject = new MyObject();
When I run my code through closure piler in advanced mode, I'm getting the warning dangerous use of the global this object
on every line where I have this.Prop =
What am I doing that's "dangerous" and how should I rewrite my code?
Thanks for your suggestions.
I have some code that looks like this:
var MyObject = function () {
this.Prop1 = "";
this.Prop2 = [];
this.Prop3 = {};
this.Prop4 = 0;
}
And then I later have this:
var SomeObject = new MyObject();
When I run my code through closure piler in advanced mode, I'm getting the warning dangerous use of the global this object
on every line where I have this.Prop =
What am I doing that's "dangerous" and how should I rewrite my code?
Thanks for your suggestions.
Share Improve this question asked Jul 1, 2012 at 1:07 frenchiefrenchie 52k117 gold badges319 silver badges527 bronze badges 1- possible duplicate of this: stackoverflow./questions/5301373/… – William Niu Commented Jul 1, 2012 at 1:11
2 Answers
Reset to default 6I would remend writing it like this:
function MyObject() {
this.Prop1 = "";
this.Prop2 = [];
this.Prop3 = {};
this.Prop4 = 0;
}
However, the real fix is to use the @constructor
JSDoc notation on the line before the constructor:
/** @constructor */
The Closure Compiler Error and Warning Reference provides detailed explanations for the warnings related to the dangerous use of this
:
- JSC_UNSAFE_THIS
- JSC_USED_GLOBAL_THIS
The warning about using the global this
object helps prevent accidentally calling a constructor function without the new
keyword, which would result in the constructor properties leaking into the global scope. However, for the piler to know which functions are intended to be constructors, the annotation /** @constructor */
is required.
版权声明:本文标题:javascript - "dangerous use of the global this object" warning in Google Closure Compiler - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206805a2432969.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论