“危险使用全球这个对象” Google Closure编译器中的警告 [英] "dangerous use of the global this object" warning in Google Closure Compiler

查看:230
本文介绍了“危险使用全球这个对象” Google Closure编译器中的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some code that looks like this:

var MyObject = function () {
  this.Prop1 = "";
  this.Prop2 = [];
  this.Prop3 = {};
  this.Prop4 = 0;
}

然后我有了这个:

var SomeObject = new MyObject();

当我在高级模式下通过闭包编译器运行代码时,我收到警告在我有 this.Prop =

When I run my code through closure compiler 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?

感谢您的建议。

推荐答案

我会推荐这样写:

function MyObject() {
  this.Prop1 = "";
  this.Prop2 = [];
  this.Prop3 = {};
  this.Prop4 = 0;
}

然而,真正的解决方法是使用 @构造函数构造函数前的行上的JSDoc表示法:

However, the real fix is to use the @constructor JSDoc notation on the line before the constructor:

/** @constructor */

这篇关于“危险使用全球这个对象” Google Closure编译器中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆