Repository navigation
onload.html:
<!DOCTYPE html>
<head>
<script type="text/javascript" src="namespace.min.js"></script>
<script type="text/javascript" src="onload.js"></script>
<script type="text/javascript">
var onLoadExample = namespace.module('org.startpad.namespace.examples.onload');
</script>
</head>
<body onload="onLoadExample.main();">
<h1>OnLoad demo</h1>
<p>Waiting for script to run...</p>
</body>
</html>
onload.js:
namespace.module('org.startpad.namespace.examples.onload', function (exports, require) {
exports.main = function () {
alert("Your page is ready.");
};
});
How do you do imports to avoid statments like:
var obj = new namespace.com.foo.bar.baz.FooBar();
I will generally have an "imports" type line at the top of my module, and then use the abbreviated name in the code. This is similar to Python's "from module import name as alias" pattern.
var library = require('com.foo.bar.baz');
...
var obj = new library.FooBar();
It's generally considered a best practice (in the Python community) to leave the external module name (like "library", above) so that someone reading your code can recognize which functions are external to your module, and which ones are local.
The Simple Optimization of the Closure Compiler works quite well with namespace.js. For example, this sample:
namespace.module('org.startpad.namespace.examples.closure', function (exports, require) {
exports.explicit = function () {
alert(1);
};
exports['quotedExplicit'] = function () {
alert(2);
};
function exported() {
local(3);
}
function local(n) {
alert('p' + n);
}
exports.extend({'exported': exported})
});
becomes (pretty-printed for clarity - normally white space is also removed):
namespace.module("org.startpad.namespace.examples.closure").define, function(a, b) {
a.explicit = function() {
alert(1)
};
a.quotedExplicit = function() {
alert(2)
};
a.extend({exported:function() {
alert("p3")
}})
});
Note that the compiler renamed the exports parameter to the shortened a. And it folded the local function into the 'exported' function. It also moved the body of exported directly into the extend function call, since it was only referenced once.
All of these changes are "safe" and don't modify the behavior of this namespace module.
Advanced Optimizations, on the other hand, will cause some issues, especially if you want to use the online Closure Compiler UI. Unmodified, the above sample becomes mangled:
namespace.c("org.startpad.namespace.examples.closure").a(function(a) {
a.b = function() {
alert(1)
};
a.quotedExplicit = function() {
alert(2)
};
a.extend({exported:function() {
alert("p3")
}})
});
If you run the Closure Compiler locally (Java app), you can place the lookup and
define functions in an [externs.js] file, to keep closure from renaming them. If
you want to use the Closure Compiler UI, you'll have to rewrite these as
namespace['lookup'] and ['define']:
namespace['module']('org.startpad.namespace.examples.closure', function (exports, require) {
...
});
The only other gottcha, is the use of an explicit property name assignment will get renamed. Note that both quotedExplicit and exported, work just fine (are not mangled).