Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closure Compiler Externs - WARNING - Property never defined on

I am preparing externs for PIXI.js library. I am getting the following warning:

js/Test.js:188: WARNING - Property position never defined on PIXI.Sprite
        button.position.y = y;

Here are the relevant extern definitions:

//UPDATE

/** 
 * @constructor
 * @extends {PIXI.Container} 
 * @param {PIXI.Texture} texture
 */
PIXI.Sprite = function(texture){};

/** 
 * @constructor
 * @extends {PIXI.DisplayObject} 
 */
PIXI.Container = function(){};

/**
 * @constructor 
 * @extends {PIXI.EventEmitter} 
 */
PIXI.DisplayObject = function(){};

/**
 * @type {PIXI.Point}
 */
PIXI.DisplayObject.position;

Still getting the same warning.

What am I doing wrong?

When I am replacing PIXI.DisplayObject.position; with PIXI.DisplayObject.prototype.position; that seems to clear the warning.

Does it mean that I should always define SomeObject.prototype.prop rather then SomeObject.prop ?

like image 842
James May Avatar asked Dec 08 '15 18:12

James May


1 Answers

This is highlighting the difference between static and prototype properties.

Given:

/**
  * @constructor
  * @param {number=} opt_num
  */
function foo(opt_num) {
    if (opt_num !== undefined) {
        this.bar = opt_num;
    }
}
foo.prototype.bar = 17;
foo.bar = 42;

We have both a static property and a prototype property of the same name. However they are referenced differently:

console.log((new foo()).bar); // 17
console.log((new foo(0)).bar); // 0
console.log(foo.bar); // 42

So in an extern, when you are defining properties on a type - you typically want to define them on the prototype object:

/** @param {foo} obj_foo */
function log(obj_foo) {
   // This is an instance of "foo".
   // The "bar" property references prototype or instance
   // properties - not static properties.
   console.log(obj_foo.bar);

   // Static properties can only be referenced by the full namespace
   console.log(foo.bar);
}
like image 125
Chad Killingsworth Avatar answered Oct 21 '22 00:10

Chad Killingsworth