1

The code below throws a warning/error when compiled with google closure compiler advanced mode.

JSC_INEXISTENT_PROPERTY: Property getJerseyNumber never defined on player

Any ideas how we can fix this?

var getDetails = {
    getJerseyNumber: function() {
    return Math.random();
    }
};

/**
 * @param {Object} source
 * @param {Object} delta
 */
function mixIn(source, delta) {
    for (var i in delta) {
        source[i] = delta[i];
    }
}
/**
 *  @type  {{name: string , sport: string}}
 */
var player = {
    name: 'Tom Brady',
    sport: 'Football'
};

mixIn(player, /** @lends {player} */ getJerseyNumber);

alert(player.getJerseyNumber()); 

Using @lends work with object literals only. So adding this line will work, but any other ideas?

mixIn(player, /** @lends {player} */ {
    getJerseyNumber: function() {
        return Math.random();
    }
 });

1 Answer 1

2

According to the Closure documentation,

@lends

Indicates that the keys of an object literal should be treated as properties of some other object. This annotation should only appear on object literals.

This is described in more detail by jsdoc-toolkit.


For your example, I would use classes for common behavior.

/**
 * @constructor
 * @param {string} name
 * @param {string} sport
 */
var Player = function(name, sport) {
    this.name = name;
    this.sport = sport;
};

Player.prototype.getJerseyNumber = function() {
    return Math.random();
};

var player = new Player('Tom Brady', 'Football');

alert(player.getJerseyNumber());

Of course, I imagine your example has some additional complexity, but without code to represent that complexity, I can only guess what that is.

You may be after the proposed (but as yet nonexistant) @mixin annotation.

1
  • The code sample provided is a good demonstration to fix the above issue, but basically i was looking for a way to have the compiler recognize multiple mixins. This is a handy construct for JS programming and is being used quite a lot (one popular code base using this is jquery). Looks like this will have to wait till this mixin feature lands in google closure compiler.
    – sbr
    Dec 29, 2014 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.