Search code examples
propertiesexternalgoogle-closure-compiler

Google Closure Compiler: Declaring properties in externs


I've just been trying to work out how to do this, and didn't find much on google, so I'll document what I found here.

I needed to define a JS class in an external library and also define some properties and method on it. I could do methods and the class, but was hitting a wall with properties.

So how is it done?


Solution

  • OK, let's imagine I've got an external JS class called 'Bob'.

    My closure externs for this would be:

    /** @constructor */ var Bob = function() {};
    

    If I had a method (flyAway(speed)) I would add:

    Bob.prototype.flyAway = function(speed) {};
    

    And if I had a property (currentSpeed) I would add:

    Bob.prototype.currentSpeed;
    

    (the last one, I was doing (stupidly in hindsight) Bob.currentSpeed;)