Sunday, 15 January 2012

javascript - Google Closure Namespace Clash -


I have a JavaScript module that has been compiled using Google Closer. This module needs to be included in the web page, which also includes some third party unrelated code.

The problem is that the pieces of both code are using the namespace "com": com.companyone.xyz and com Companytwo.xyz

It seems that the closed compiler automatically Generating code to create namespace in the way:

  var com = {mycompany: {}};  

And so whatever is already used com namespace is written on it.

If I want to write it by hand, then I know that the name space already exists or not:

  var com = com || {}; Com.mycompany = com.mycompany || {};  

As a temporary solution, I am thinking of changing from "var com = {"} to "var com = com" {", but it seems to be hacking.

Is there any way to ensure that the Closer Compiler does not overwrite the existing ComNamespace?

< P> A good practice is to separate your definitions to interfere with as few people as possible. <

Take a new one for this variable For example:

  var com = 'example'; (function () {var com = {mycompany: {}};}) ();  

inside the scope com does not affect one outside of it.

If you need to provide global variables - like, say, global Use com , goog.global :

  goog.global.com  

if You need to "export" your variable at global peak, then you can use goog.exportSymbol It preserves the defined namespaces from.


Although I am really unsure about cases where the compiler will generate the code you posted; An example would be useful.


No comments:

Post a Comment