tag:blogger.com,1999:blog-4222197394079314848.post3071809175336157818..comments2023-05-10T02:34:45.071-05:00Comments on Mark A. Ziesemer: JavaScript namespace functionMark A. Ziesemerhttp://www.blogger.com/profile/12752422620777325409noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-4222197394079314848.post-21281342261545035702012-11-04T16:40:34.633-06:002012-11-04T16:40:34.633-06:00Michal - another way to write this would have been...Michal - another way to write this would have been:<br /><br />if(o[ns[i]]){<br /> o = o[ns[i]];<br />}else{<br /> o[ns[i]] = {};<br /> o = o[ns[i]];<br />}<br /><br />As is / in short-hand form:<br /><br />o = o[ns[i]] = o[ns[i]] || {};<br /><br />The first portion:<br /><br />o = o[ns[i]] = <br /><br />Assigns the end result simultaneously to both o and o[ns[i]].<br /><br />The remaining Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-43330561866780372782012-11-04T16:30:00.453-06:002012-11-04T16:30:00.453-06:00Hi,
could you explain how exactly this piece of co...Hi,<br />could you explain how exactly this piece of code is supposed to work?<br /><br />o = o[ns[i]] = o[ns[i]] || {};Michal Zimmermannhttps://www.blogger.com/profile/12962615521109080831noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-33312965842747829042012-03-09T05:03:01.462-06:002012-03-09T05:03:01.462-06:00That is so simple and elegant, I am using it in my...That is so simple and elegant, I am using it in my project...Thanks!johnbkhttps://www.blogger.com/profile/03354826158910507790noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-8878550591923012992011-09-09T14:54:27.789-05:002011-09-09T14:54:27.789-05:00Anonymous - I am using this code extensively with ...Anonymous - I am using this code extensively with IE7, and haven't observed any issues. You will need to provide additional details - such as any errors received, expected vs. observed results, etc.Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-583516511598197952011-09-09T14:21:07.109-05:002011-09-09T14:21:07.109-05:00IE7 seems to break on this line:
o = o[ns[i]] = o[...IE7 seems to break on this line:<br />o = o[ns[i]] = o[ns[i]] || {};Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-91144590327978878422011-04-01T11:09:08.737-05:002011-04-01T11:09:08.737-05:00Hi, I added a recursive twist to your approach:
v...Hi, I added a recursive twist to your approach:<br /><br />var _ = function(name, container) {<br /> var ns = name.split('.');<br /> var o = container || window;<br /> var i = 0;<br /> var len = ns.length;<br /> for(; i < len; ++i) {<br /> o = o[ns[i]] = o[ns[i]] || {};<br /> }<br /> o._ = function(name){ return window._(name, o);}<br /> return o;<br />};<br /><br />If I binarezhttp://www.binarez.comnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-25319927718980340392011-02-04T08:44:08.995-06:002011-02-04T08:44:08.995-06:00Antonio - I completely support your effort, as I i...Antonio - I completely support your effort, as I insist that all my code is warning-free as well. That said, I'm not able to reproduce the issue that you're observing. Are you seeing it in the initial evaluation of the function, or in an execution of the function? If an execution, can you please provide an example call that demonstrates the warning you observed? Thanks!Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-78060294900078769252011-02-03T09:55:07.175-06:002011-02-03T09:55:07.175-06:00Hi there,
Running the following code gives me a w...Hi there,<br /><br />Running the following code gives me a warning in Firefox 3.6.13:<br /><br />==== code ====<br />var namespace = function(name, separator, container){<br /> var ns = name.split(separator || '.'),<br /> o = container || window,<br /> i,<br /> len;<br /> for(i = 0, len = ns.length; i < len; i++){<br /> o = o[ns[i]] = o[ns[i]] || {};<br /> }<br /> Antonionoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-69070578845551907922010-05-03T05:52:27.740-05:002010-05-03T05:52:27.740-05:00Hi!
This namespace function is a great idea. But ...Hi!<br /><br />This namespace function is a great idea. But is there a way to add private variables and functions ?<br /><br />Dustin Diaz does it with his namespace code : http://www.dustindiaz.com./namespace-your-javascript/<br /><br />Anyway, keep up the good work!Daishi Kaszerhttp://daishi.kaszer.free.fr/noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-70564669586062173842009-12-27T19:13:50.474-06:002009-12-27T19:13:50.474-06:00Anonymous: Not necessarily implemented once per f...Anonymous: Not necessarily implemented once per file, but once per scope. On the web and in XUL, this is pretty much once per window. I would always declare it using "var". Whether it becomes global or not depends upon the scope in which it is defined. For now, I would declare this within a closure / own function block, and then create the namespaces while you still have a valid Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-59024622249806718742009-12-18T19:43:04.962-06:002009-12-18T19:43:04.962-06:00So do I have to implement the function in every ja...So do I have to implement the function in every javascript file?<br /><br />It appears that if the namespace function is declared as namespace = function that it becomes a global so I can then use it from all of my javascript files.<br /><br />The problem is that it then pollutes the global name space. On the other hand if I implement it as var namespace = function then I have to have it in everyAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-4566936271291428292009-09-20T15:06:49.741-05:002009-09-20T15:06:49.741-05:00jpswain09 - Sorry for the late response, but an ex...jpswain09 - Sorry for the late response, but an example is now included.Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-38060024179771270072009-05-18T15:08:00.000-05:002009-05-18T15:08:00.000-05:00Oh, dang; pardon my restating the update section a...Oh, dang; pardon my restating the update section above.Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-65468067552756138492009-05-18T14:51:00.000-05:002009-05-18T14:51:00.000-05:00Mark, in javascript, any variable that is not expl...Mark, in javascript, any variable that is not explicitly declared with the "var" keyword will become global, wherever you defined it. This is one of javascript's most unfortunate big warts. Try this sequence of inputs in the Firebug console if you want to convince yourself -- the latter "len" will be 12 in the global environment:<br /><br />len; i; function oops() { len = 1; var i = 0; } oops(); Johan Sundströmhttps://www.blogger.com/profile/04076097346172610543noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-38638205266937679442009-04-19T20:46:00.000-05:002009-04-19T20:46:00.000-05:00Mark,
It's really cool of you to post this namesp...Mark,<br /><br />It's really cool of you to post this namespacing technique. However, as an intermediate Java programmer who is pretty new to JavaScript, I'm pretty confused about how to make use of the namespace function to ensure that my classes all stay within my defined namespace. Is there any way you could post a zip file or something of a mini-quickstart app that just shows 3 js files Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-55782546789197063962008-11-23T19:16:00.000-06:002008-11-23T19:16:00.000-06:00It may not be exactly clear, but it is already bei...It may not be exactly clear, but it is already being checked for.<BR/><BR/>Look at the part within the for loop. If the object exists, it is used, otherwise a new object ({}) is used.<BR/><BR/>I just looked at your link, and it does this part exactly the same way. However, it is also hard-coded to check against a "DTS" parent, which I'm not at all a big fan of - as noted in the Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-19296103362558605772008-11-23T13:51:00.000-06:002008-11-23T13:51:00.000-06:00shoudnt we check weather the namespace is already ...shoudnt we check weather the namespace is already created or not? nice code though!<BR/>I found another nice example here: http://www.dottostring.com/2008/11/how-to-namespace-your-javascript/Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-13149821522018838972008-05-13T23:10:00.000-05:002008-05-13T23:10:00.000-05:00remy - please correct me if you still think I'm in...remy - please correct me if you still think I'm incorrect, but the documentation I'm reading and the examples I've run show that the "len" variable is in the same (local) scope as "i".<BR/><BR/>I'm adding to the original post to make use of formatting that isn't supported within comments here.Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-34787866816328627752008-05-13T17:33:00.000-05:002008-05-13T17:33:00.000-05:00By the way - the 'len' variable is on the global s...By the way - the 'len' variable is on the global scope ;-)Anonymousnoreply@blogger.com