0

I have private function createSomething():

function Player(id) {

  /**
   *  Creates stuff
   *  @private
   */
  this.createSomething = function() {
    // do something good
  };
}

and I want to see the renamed function "createSomething()" after compiling the source with Google Closure Compiler. Yes, I know about ADVANCED_OPTIMIZATIONS but it is incompatible with jQuery and other libraries.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Vlad
  • 442
  • 5
  • 15

2 Answers2

3

The solution is to use a string literal to refer to the property.

function Player(id) {
  /**
   *  @private
   */
  this['createSomething'] = function() {
    // do something good
  };
}

This works because the compiler never renames string literals. But be careful.

You can compile your code with ADVANCED_OPTIMIZATIONS and still have you compatibility with other libraries. You'll need to read about externs and exports in the library documentation:

Rich Dougherty
  • 3,231
  • 21
  • 24
-3

Just use without this

function Player(id) {

  /**
   *  Creates stuff
   *  @private
   */
  createSomething = function() {
    // do something good
  };
}
Vlad
  • 442
  • 5
  • 15
  • 2
    I believe this creates a function named createSomething in the global space, both in the source and in the Closure output. While this does retain the original name in the compiled version, I'm not sure this is what you want. This is going to create confusion if anyone else ever uses a function named "createSomething," either declaring it or calling it. It's also not going to have a this object in its local context that refers to Player (this will refer to the global window instead). – Chris Moschini Oct 19 '11 at 19:41
  • This answer is wrong for the question and if it works it is by accident. Then other answer by Rich is the correct way according to Google that built the compiler. – David Mårtensson Mar 19 '12 at 14:49