在JavaScript中为Google闭包编译器注释单例对象,或者“危险地使用全局this对象”.警告

Annotate Singleton objects in JavaScript for the Google Closure Compiler, or "dangerous use of the global this object" warning

本文关键字:对象 危险 全局 警告 this Google 中为 JavaScript 闭包 编译器 单例      更新时间:2023-09-26

我正在使用Google Closure Compiler in ADVANCED_OPTIMIZATIONS编译级别,并且已经开始注释我的构造函数,因为我得到了各种各样的警告:

警告-使用全局this对象

是危险的

对于我的"构造函数"类型的函数,我将像这样注释它们:

/**
 * Foo is my constructor
 * @constructor
 */
Foo = function() {
   this.member = {};
}
/**
 * does something
 * @this {Foo}
 */
Foo.prototype.doSomething = function() {
   ...
}

这似乎工作得很好,但是,如果我有一个'单例'对象,不是用var myFoo = new Foo()构造;我在文档中找不到如何注释这种类型的对象因为它的类型就是对象,对吧?

Bar = {
   member: null,
   init: function() {
      this.member = {};
   }
};

在Closure中创建单例的首选方式如下:

/** @constructor */
var Bar = function() { };
goog.addSingletonGetter(Bar);
Bar.prototype.member = null;
Bar.prototype.init = function() {
  this.member = {};
};

允许单例的延迟实例化。像这样使用:

var bar1 = Bar.getInstance();
var bar2 = Bar.getInstance();
bar1.init();
console.log(bar2.member);

请记住,这并不妨碍人们使用构造函数来创建Bar的实例。

正是"危险使用This "警告您要注意的潜在错误类型。在您的示例中,闭包编译器可能会尝试将您的代码"扁平化"为:

Bar$member = null;
Bar$init = function() { this.member = {}; };

注意:闭包编译器目前不会扁平化被声明为全局对象的命名空间(即前面没有"var"关键字),所以你的代码现在可能仍然可以工作。然而,没有人知道它不会在未来的版本中这样做,你的代码会在没有警告的情况下突然中断。

当然,那么"Bar$member"answers"Bar$init"将分别重命名为"a"answers"b"。这被称为"命名空间平坦化"或"属性折叠"。

您可以立即看到您的代码不再正常工作。在编译之前,如果您写:

Bar.init();

thisBar。但是,编译后它变成:

Bar$init();

this将不再指代Bar。而是指向全局对象。

这是编译器试图警告你以这种方式使用"This"是"危险的",因为"This"可能被更改为指向"global"对象。这才是警告的真正含义。

简而言之,不要这样做。这种类型的编码风格产生的bug 非常难以追踪。

这样修改你的代码:

var Bar = {    // Closure Compiler treats globals and properties on global differently
  member: null,
  init: function() { Bar.member = {}; }
};

或使用闭包:

var Bar = (function() {
  var member = null;
  return {
    init: function() { member = {}; }
  };
})();

在高级模式下使用闭包编译器时,不要试图通过注释来消除警告。警告的存在是有原因的——它们试图警告你一些事情。