Displaying dynamically created functions on objects with a closure compiler
I am trying to annotate my javascript so that closing does not rename all characters as I am working with vanilla javascript.
/**
* @constructor
* @expose
* @type{foo}
*/
foo = function (el, args) {
"use strict";
var s = "Hello World";
/*
* @expose
* @this {foo}
* @type {function}
*/
this.introduce = function () {
return s;
};
};
However, the generated output when I run it through the advanced optimization closure compiler is
foo = function() {
this.a = function() {
return"Hello World"
}};
How can I request a closure to keep the entered name as this will be called from external javascript.
source to share
The following options can be used to prevent the compiler from renaming Closure symbols:
-
@export
(also see:goog.exportSymbol
orgoog.exportProperty
) - Export symbols, storing them in the global object referenced by the string. See Export the symbols you want to keep
- Define an interface in externs file and implement the interface (see related answers below)
- Request properties
- Note:
@expose
deprecated
If you don't want to define the methods of the function prototype, as shown in For example, you can export the constructor foo
with goog.exportSymbol
and use @expose
to export the methods.
/**
* @param {Element} el
* @param {...*} args
* @constructor
*/
var foo = function (el, args) {
"use strict";
/** @private */
this.msg_ = "Hello World";
/**
* @this {foo}
* @return {string}
* @expose
*/
this.introduce = function () {
return this.msg_;
};
};
goog.exportSymbol('foo', foo);
By defining the methods of a function prototype, it goog.exportSymbol
can be used to export both constructor and method names.
/**
* @param {Element} el
* @param {...*} args
* @constructor
*/
var foo = function (el, args) {
"use strict";
/** @private */
this.msg_ = 'Hello World!';
};
goog.exportSymbol('foo', foo);
/**
* @return {string}
*/
foo.prototype.introduce = function () {
return this.msg_;
};
goog.exportSymbol('foo.prototype.introduce', foo.prototype.introduce);
See related questions on stackoverflow:
- Why does the Closure compiler rename external type properties?
- Is it possible to tell the Closure compiler only for certain types to stop renaming properties?
- John will answer the question: How to tell the Closure compiler to persist object properties
source to share