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.

+1


source to share


1 answer


The following options can be used to prevent the compiler from renaming Closure symbols:

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:

+4


source







All Articles