Javascript - is there any hard limit (or performance impact) for namespace nesting? -
i'm trying learn bit more javascript apart typical var x = function(){...}
constructs, gone namespaces.
in php, work namespaces avoid collisions , organize constants, classes , functions. far, i've done basic namespacing this:
var helpers = { strings: { add: function(a, b) { alert(a + ' plus ' + b + ' equals ' + (a + b)); }, msgbox: function(text) { alert(text); } } }
so can write html blocks this:
<button class="ui-button" type="button" onclick="helpers.strings.msgbox('hello, world!');"><img src="assets/images/alert.png" alt="alert"> click me!</button>
my questions are:
- is there practical/hard limit number of levels can nest namespaces within?
- is there performance impact associated level of nesting given function?
- can extend given namespace later in time? like... having
core.js
file , extendingstrings
namespace adding more functions in, let's say,extended.js
?
i'm not going build horribly nested structure or know if there practical limitations imposed browser engine or language itself, question more of theorical nature (i'm not building construct test this, in case).
is there practical/hard limit number of levels can nest namespaces within?
obviously there because if nothing else more levels require more memory , memory finite, , in practice there other restrictions in place (derived implementation details of each particular javascript engine).
but practical answer is: if have reason believe might go near these limits, doing wrong.
is there performance impact associated level of nesting given function?
yes, because each level of indirection involves finding next nested "namespace" object in memory , looking properties. in practice cost infinitesimal compared other stuff code doing, not able measure difference unless number of levels large , digging nested value within loop.
for example, not best of ideas:
for(var = 0; < 1000000000; ++i) { ns1.ns2.ns3.ns4.ns5.ns6.ns7.ns8.ns9.ns10.ns11.ns12.ns13.count += 1; }
fortunately if ever need there simple workaround:
var ns13 = ns1.ns2.ns3.ns4.ns5.ns6.ns7.ns8.ns9.ns10.ns11.ns12.ns13; for(var = 0; < 1000000000; ++i) { ns13.count += 1; }
can extend given namespace later in time? like... having core.js file , extending strings namespace adding more functions in, let's say, extended.js?
you can, have careful both of these files use mechanism injecting variables namespace not replace contents of namespace.
Comments
Post a Comment