Question

google-closure-compiler in advanced mode, warns for the following code. This is caused by namespace flattening.

var com.my.test.namespace = {};
com.my.test.namespace.a = new Class ({
    name : "test",
    initialize : function() {   //constructor
        alert(this.name);
    }
});

com.my.test.namespace.a();

When running the google-closure-compiler in ADVANCED mode with debug enabled, com.my.test.namespace.a is replaced to com$my$test$namespace$a

So, the minified code is roughly the following,

var com.my.test.namespace = {};
com$my$test$namespace$a = new Class ({
    name : "test",
    initialize : function() {   //constructor
        alert(this.name);
    }
});

com$my$test$namespace$a();

When, com$my$test$namespace$a() is invoked, "this" isn't com.my.test.namespace anymore, it is the window, and hence the compiler warning.

some documentation suggests to replace "this" with com.my.test.namespace.a to get around this issue. But, is that the right thing to do? what does com.my.test.namespace.a.name point to? It surely does not seem like the current instances "name" property.

What is the right way to do it? com.my.test.namespace.a.prototype.name ?

PS:
I am using mootools library for the Class method.
Was it helpful?

Solution

If initialize is the constructor, then the "this" should be name of the type not the namespace. Here is how this should be annotated for the compiler

/** @const */
var namespace = {};

/** @constructor */
namespace.a = new Class (/** @lends {namespace.a.prototype} */{
    name : "test",
    initialize : function() {
        alert(this.name);
    }
});

new namespace.a();

Here is what I have:

  1. a const namespace. Not required but improves type checking.
  2. a constructor declaration (introduces a type name)
  3. @lends on the object literal, indicates that the properties are being used in a type declaration.

@lends is necessary here because the Closure Compiler doesn't have any specific knowledge built in about Moo tools' Class declaration and you need to help it understand that it is being used as part of the type definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top