Spa*_*kup 2 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;
}
Run Code Online (Sandbox Code Playgroud)
这将调用这样的类:
/**
* @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 = {};
...
}
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
JSC_UNSAFE_NAMESPACE: incomplete alias created for namespace Editor.Elements
Run Code Online (Sandbox Code Playgroud)
指的是这两行:
if(typeof Editor.Elements[element] === 'function') { /* ... */ } // 1.
return new Editor.Elements[element](view).create(x, y); // 2.
Run Code Online (Sandbox Code Playgroud)
即使我要删除第一个警告,也无法摆脱第二个警告。有什么办法可以通过注释解决此问题?
该警告来自“折叠属性”优化过程。编译器警告您可能会发生以下转换:
Editor$Elements$Circle = function(view) { ... }
Run Code Online (Sandbox Code Playgroud)
在这种情况下,访问Editor.Elements['Circle']
将失败,因为Circle
它不再是的属性Editor$Elements
。
这还会导致一致的属性访问问题:
var element = type[0].toUpperCase() + type.slice(1);
if(typeof Editor.Elements[element] === 'function') {
Run Code Online (Sandbox Code Playgroud)
这种情况相当于Editor.Elements['Circle']
用引用的名称访问属性,并且原始格式为点缀。编译器可以重命名点分访问,并将引号访问保留下来,从而破坏您的代码。
归档时间: |
|
查看次数: |
680 次 |
最近记录: |