简体   繁体   中英

Closure compiler Advanced Optimization prevent object property renaming

Assume the following code.

1) In the ClosureTestClass constructor, we get an object passed from the external system. In case if the property1 of inputObjectFromExternalSystem is renamed in this code, the object sent from external system will not have the renamed property. Is there a way to specify the compiler not to rename the properties of a particular object.

I know we can do by using

inputObjectFromExternalSystem['property1'].

I am looking for something in the comments like /** @expose */. I tried with /** @lends */ but It didnt work.

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print
// @use_closure_library true
// ==/ClosureCompiler==


/** @export */
var ClosureTestClass = (function () {
    function ClosureTestClass(inputObjectFromExternalSystem) {
        this.property1 = inputObjectFromExternalSystem.property1;
        this.property2 = inputObjectFromExternalSystem.property2;
    }
/** @expose */
    ClosureTestClass.prototype.printProperty = function () {
        console.log(this.property1);
        console.log(this.property2);
    };
    return ClosureTestClass;
})();

/** @export */
var ClosureTestClassUse = (function () {
    function ClosureTestClassUse () {
        this.closureTest = new ClosureTestClass;
    }
    /** @expose */
    ClosureTestClassUse.prototype.print = function () {
        console.log(this.closureTest);
    };
    return ClosureTestClassUse;
})();

var a = new ClosureTestClassUse();

2) what is the use case for /** @lends */

@export is designed to prevent renaming - and is the only supported annotation that accomplishes this. You must also use the --generate_exports flag and your source code must either include the base.js file from Closure-library or contain definitions for define both goog.exportSymbol and goog.exportProperty with the same method signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM