05.05.05
JS Namespaces redux
Ryan blogged about a Suggestion for Emulating Namespaces in JavaScript that Daniel dug up for me.
One of the things Ryan is afraid of, and the one of the first things I thought of when I read the suggestion on namespaces, is that . is all figured out at runtime (this being javascript there tends not to be much other time). As Ryan mentioned though, you can get a reference to make it cheaper. For example (his example):
// CD.Music must be loaded already. Create the constructor.
CD.Music.Classical = function () {}
// Inherit from CD.Music.
CD.Music.Classical.prototype = CD.Music.prototype; // Inheritance.
// Add to the class and/or override as necessary.
CD.Music.Classical.prototype.composer = null;
CD.Music.Classical.prototype.orchestra = null;
CD.Music.Classical.prototype.conductor = null;
CD.Music.Classical.prototype.soloist = null;
Is (AFAIK) equivalent to:
// CD.Music must be loaded already. Create the constructor.
CD.Music.Classical = function () {}
// Inherit from CD.Music.
CD.Music.Classical.prototype = CD.Music.prototype; // Inheritance.
// Add to the class and/or override as necessary.
cmcp = CD.Music.Classical.prototype;
cmcp.composer = null;
cmcp.orchestra = null;
cmcp.conductor = null;
cmcp.soloist = null;
Which should help reduce that penalty. But to answer your question directly: “No, the client doesn’t have anything better to do than evaluate our inefficient code.” ;-)