Google Closure Compiler中"危险使用全局此对象"警告

fre*_*hie 3 javascript google-closure-compiler

我有一些看起来像这样的代码:

var MyObject = function () {
  this.Prop1 = "";
  this.Prop2 = [];
  this.Prop3 = {};
  this.Prop4 = 0;
}
Run Code Online (Sandbox Code Playgroud)

然后我有了这个:

var SomeObject = new MyObject();
Run Code Online (Sandbox Code Playgroud)

当我在高级模式下通过闭包编译器运行我的代码时,我会dangerous use of the global this object在我拥有的每一行上收到警告this.Prop =

我在做什么,这是"危险的",我应该如何重写我的代码?

谢谢你的建议.

Ry-*_*Ry- 6

建议像这样写:

function MyObject() {
  this.Prop1 = "";
  this.Prop2 = [];
  this.Prop3 = {};
  this.Prop4 = 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,真正的解决方法是@constructor在构造函数之前的行上使用JSDoc表示法:

/** @constructor */
Run Code Online (Sandbox Code Playgroud)