Google Closure Compiler为命名空间创建的别名不完整

时间:2015-02-20 15:19:04

标签: javascript namespaces google-closure-compiler

我遇到了Closure Compiler的问题。这是我的功能:

Editor.Elements.Map = function(type, view, x, y) {
    var element = type[0].toUpperCase() + type.slice(1);
    if(typeof Editor.Elements[element] === 'function') {
        return new Editor.Elements[element](view).create(x, y);
    }
    return null;
}

哪个会叫这样的课:

/**
 *  @constructor
 *  @extends {Editor.Elements.Element}
 *  @param view (object) {Editor.View}
 */
Editor.Elements.Circle = function(view) {

    Editor.Elements.Element.apply(this, arguments);

    if (!(view instanceof Editor.View)) {
        throw new Error('An Instance of Editor.View is required.');
    }

    var me = this, _me = {};

    ...
}

我收到以下警告:

JSC_UNSAFE_NAMESPACE: incomplete alias created for namespace Editor.Elements

这是指这两行:

if(typeof Editor.Elements[element] === 'function') { /* ... */ } // 1.
return new Editor.Elements[element](view).create(x, y);          // 2.

即使我要删除第一个警告,我也无法驾驶第二个警告。有什么方法可以用注释解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

警告来自Collapse Properties优化传递。编译器警告您可能会发生以下转换:

Editor$Elements$Circle = function(view) { ... }

在这种情况下,访问Editor.Elements['Circle']会失败,因为Circle不再是Editor$Elements的属性。

这也会导致一致的属性访问问题:

var element = type[0].toUpperCase() + type.slice(1);
if(typeof Editor.Elements[element] === 'function') {

这种情况相当于Editor.Elements['Circle'],它通过引用的名称访问属性,原始形式是点缀的。编译器可以重命名虚线访问,并且仅保留引用的访问权限,从而破坏您的代码。

相关问题