How to tell Closure Compiler to persist object properties

I have an object declared like this:

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

      

I use my.namespace.my.object

to keep track of what functionality is available / implemented in my code. Each new version will have a modified set of features. A third party using my collapsed code will want to know what they can do in the version they have, so I provide the following exported function so 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;
}

      

The problem is that the properties are renamed when Closure Compiler starts.

My question is, what's the best way to preserve these properties? I know I can export property, but it feels messy for some reason. Is there a good closure practice to preserve object properties?

+1


source to share


3 answers


The export happens for any reason - like a Closure library directive that is used by external agents, so it cannot be renamed. In the sections here on export, explain how to make Closure keep the symbol unchanged (no renaming). You basically just need to follow the directions here. There is nothing dirty about exporting. This is exactly what you need to tell Closure that this symbol is in use by an external agent and cannot be renamed.

Another trigger that can keep Closure from renaming a property is accessing it with a line like this:



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

      

In this case, Closure sees that your code is using strings to access the property and (since it never mixes with string values) it realizes that it cannot rename the FIRST_FEATURE property safely.

+1


source


In ADVANCED mode, simply citing the keys tells the compiler not to rename them:

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

      

Useful information about renaming properties:



https://developers.google.com/closure/compiler/docs/api-tutorial3

https://github.com/google/closure-compiler/wiki/FAQ#some-of-my-properties-are-getting-renamed-but-some-arent-why

+4


source


The Closure compiler has JavaDoc style markup ...
Here's a page referencing the markup: JavaScript annotation for the Closure compiler
To preserve the field name, place /** @expose */

before the field declaration.

my.namespace.FEATURES = {
    /**@expose*/
    FIRST_FEATURE: "first feature",
    /**@expose*/
    SECOND_FEATURE: "second feature"
};

      

... if you need to preserve the namespace, it's the same concept ...

/**@expose*/
my = my || {};
/**@expose*/
my.namespace = my.namespace || {};
/**@expose*/
my.namespace.FEATURES = {
    /**@expose*/
    FIRST_FEATURE: "first feature",
    /**@expose*/
    SECOND_FEATURE: "second feature"
};

      

Apart from storing the name, it also allows you to refer to that field using dot notation in your code. By using obj["field"]

, the compiler will lose the reference if you call it obj.field

later, instead of using strings.

+4


source







All Articles