Javascript doesn't have stand-alone namespaces. It has functions, which can provide scope for resolving names, and objects, which can contribute to the named data accessible in a given scope.
Here's your example, corrected:
var namespaces = { com: { example: { /* example.com's data */ } } }
This is a variable namespaces
being assigned an object literal. The object contains one property: com
, an object with one property: example
, an object which presumably would contain something interesting.
So, you can type something like namespaces.com.example.somePropertyOrFunctionOnExample and it'll all work. Of course, it's also ridiculous. You don't have a hierarchical namespace, you have an object containing an object containing an object with the stuff you actually care about.
var com_example_data = { /* example.com's data */ };
That works just as well, without the pointless hierarchy.
Now, if you actually want to build a hierarchy, you can try something like this:
com_example = com_example || {};
com_example.flags = com_example.flags || { active: false, restricted: true};
com_example.ops = com_example.ops || (function()
{
var launchCodes = "38925491753824"; // hidden / private
return {
activate: function() { /* ... */ },
destroyTheWorld: function() { /* ... */ }
};
})();
...which is, IMHO, reasonably concise.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…