如何告诉Closure Compiler保留对象的属性 [英] How to tell Closure Compiler to preserve properties on an object

查看:67
本文介绍了如何告诉Closure Compiler保留对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样声明的对象:

I have an object declared like this:

my.namespace.FEATURES = {
    FIRST_FEATURE = "first feature",
    SECOND_FEATURE = "second feature"
};

我使用my.namespace.my.object跟踪代码中可用/实现的功能.每个新发布的版本都将具有一组经过修改的功能.使用我的最小化代码的第三方将想知道他们在所拥有的版本中可以做什么,因此我提供了以下功能,该功能已导出,以便他们知道他们可以做什么.

I use my.namespace.my.object to keep track of what kinds of features are available/implemented in my code. Every newly released version will have a modified set of features. A third-party using my minimized code will want to know what they can do in the version they have, so I supply the following function, which is exported, so that they know what they can do.

my.namespace.hasFeature = function(feature) {
    for(var prop in my.namespace.FEATURES) {
        if(my.namespace.FEATURES[prop] == feature) {
            return true;
        }
    }
    return false;
}

问题是当我运行Closure Compiler时属性被重命名.

The problem is that the properties are getting renamed when I run Closure Compiler.

我的问题是:保留这些属性的最佳方法是什么?我知道我可以导出该属性,但是由于某种原因,它感觉有点脏.是否有Closure最佳实践来保留对象的属性?

My question is: what's the best way to keep those properties preserved? I know I can export the property, but it feels kind of dirty for some reason. Is there a Closure best practice to preserve the properties of an object?

推荐答案

存在导出是有原因的-作为Closure库的指令,外部代理使用了此库,因此不应重命名. 此处有关导出的部分介绍了如何强制Closure保留符号完好无损(不重命名).您通常只需要按照此处的指示进行操作即可.导出没有任何肮脏".它正是您所需要的-告诉Closure该符号由外部代理使用,不能重命名.

Exporting is there for a reason - as a directive to the Closure library that this is used by outside agents so it should not be renamed. The sections here on exporting explain how you can force Closure to keep a symbol intact (with no renaming). You mostly just need to follow the directions here. There is nothing "dirty" about exporting. It is there for exactly what you need - to tell Closure that this symbol is used by an external agent and cannot be renamed.

另一个阻止Closure重命名属性的触发器是,是否可以通过这样的字符串访问它:

The other trigger that can keep Closure from renaming a property is if it is accessed by a string like this:

var f = "FIRST_FEATURE";
my.namespace.FEATURES[f] = "first feature";

在这种情况下,Closure看到您的代码正在使用字符串来寻址属性,并且(因为它永远不会与字符串值混淆),因此它意识到无法安全地重命名FIRST_FEATURE属性.

In that case Closure sees that your code is using strings to address a property and (since it never messes with string values) it realizes that it can't rename the FIRST_FEATURE property safely.

这篇关于如何告诉Closure Compiler保留对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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