tag:blogger.com,1999:blog-4222197394079314848.post7529161154058402421..comments2023-05-10T02:34:45.071-05:00Comments on Mark A. Ziesemer: Respecting the JavaScript global namespaceMark A. Ziesemerhttp://www.blogger.com/profile/12752422620777325409noreply@blogger.comBlogger32125tag:blogger.com,1999:blog-4222197394079314848.post-36779826516053317572012-10-21T07:02:42.097-05:002012-10-21T07:02:42.097-05:00very nice post. thanks a ton..very nice post. thanks a ton..Pratik Sonihttps://www.blogger.com/profile/13042614725053449995noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-88348528520852928032010-09-07T08:37:57.754-05:002010-09-07T08:37:57.754-05:00Hi Mark,
I do have a concern with global namespac...Hi Mark,<br /><br />I do have a concern with global namespace and javascript modules.<br />I'm using your rules for my add-on, but now I want to isolate part of my code in a javascript module.<br /><br />So I have all my javascript code wrapped into the following object for extension QuickFileMail :<br />if(!net) var net={};<br />if(!net.phpconcept) net.phpconcept={};<br />if(!Vincenthttp://www.phpconcept.netnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-67680666528917983152010-09-02T21:51:22.823-05:002010-09-02T21:51:22.823-05:00Nick: You're completely correct - using the &...Nick: You're completely correct - using the "let" statement introduced with JavaScript 1.7 is a great alternative. This should work great for use within Firefox extensions, but for use within web pages, the only web browser currently supporting JavaScript 1.7 is also Mozilla Firefox.<br /><br />However, the object namespacing approach still serves a purpose, even when "let&Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-84002887686454721542010-09-02T21:42:09.211-05:002010-09-02T21:42:09.211-05:00Ste / gigamorph:
Regarding the "JavaPackage&...Ste / gigamorph:<br /><br />Regarding the "JavaPackage" issues under Mozilla Rhino, please refer to <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=468385" rel="nofollow">https://bugzilla.mozilla.org/show_bug.cgi?id=468385</a>.<br /><br />As a quick work-around, one can just evaluate "delete" statements during the Rhino initialization to remove these object references, Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-79900904401312446972010-07-20T05:24:29.059-05:002010-07-20T05:24:29.059-05:00Doesn't using 'let' rather than 'v...Doesn't using 'let' rather than 'var' in Javascript 1.7 within extensions avoid polluting the global namespace in an easier manner? see: https://developer.mozilla.org/en/new_in_javascript_1.7Nickhttps://www.blogger.com/profile/16375008289084529344noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-64384425003754688282010-04-21T13:45:29.243-05:002010-04-21T13:45:29.243-05:00Ste: I have the same problem.
The problem seems t...Ste: I have the same problem.<br /><br />The problem seems to be Rhino pre-defines java, com, org, net, and edu as Java packages and assumes any reference relative to them to be also a Java Package.<br /><br />Thus com, com.ziesemer, com.ziesemer.myPackage all evaluate as JavaPackage instead of undefined so none of the first three lines get effectively executed.<br /><br />It should work if you Anonymoushttps://www.blogger.com/profile/14897419252527123691noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-62988070429024785862010-03-20T09:58:10.411-05:002010-03-20T09:58:10.411-05:00Hi Mark, great article! I wonder if it is still va...Hi Mark, great article! I wonder if it is still valid with ECMAScript 1.6 implemention "Mozilla Rhino" 1.6 release 2.<br />I cut and pasted the updated sample and I get the following error running it inside jrunscript:<br /><br />[ste ~] o(= jrunscript /tmp/test.js <br />script error in file /tmp/test.js : sun.org.mozilla.javascript.EcmaError: TypeError: Cannot call property alertHello Stehttps://www.blogger.com/profile/04439244082106147350noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-75438130461105336942010-02-20T09:16:16.443-06:002010-02-20T09:16:16.443-06:00Vikram - the code you posted by itself is valid, t...Vikram - the code you posted by itself is valid, though I assume that you would replace "namespace" with something more unique.<br /><br />See my response to Rez, just above your comment, as it looks like you two are having the exact same issue.Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-33612329309726224942010-02-20T09:13:13.003-06:002010-02-20T09:13:13.003-06:00Ravi: com.ziesemer.myfun(); Will work from anywher...Ravi: com.ziesemer.myfun(); Will work from anywhere, including from within any other function.Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-61286097530698347742010-02-20T02:20:55.798-06:002010-02-20T02:20:55.798-06:00if(!com) var com={};
if(!com.ziesemer) com.zieseme...if(!com) var com={};<br />if(!com.ziesemer) com.ziesemer={};<br /><br />com.ziesemer.myPackage = function(){<br /> var pub = {};<br /> <br /> pub.name = "visitor";<br /> pub.greeting = "hello " + pub.name;<br /> pub.alertHello = function(){<br /> alert(pub.greeting);<br /> };<br /> return pub;<br />}();<br /><br />com.ziesemer.myfun = function()<br />{<br /> var tst Ravihttps://www.blogger.com/profile/01948365714245658194noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-49982392117094005622009-12-30T06:42:49.193-06:002009-12-30T06:42:49.193-06:00hello everyone !!
I'm like a beginner in jav...hello everyone !! <br /><br />I'm like a beginner in java Script and restructuring one of my Firefox addon for global Namespacing.<br /><br />------------------<br />var namespace = new function() {<br /> return {<br /> i: 0,<br /> var: 'bar',<br /> <br /> function1: function(){ <br /> alert("inside function 1"); <br /> namespace.function2();Vikram Baghelhttps://www.blogger.com/profile/00805511834102909968noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-91316894196512040972009-07-28T12:34:51.954-05:002009-07-28T12:34:51.954-05:00Rez - it's difficult to provide a complete ans...Rez - it's difficult to provide a complete answer without being able to view the complete extension, but it sounds like you're working with 2 separate scopes (possibly different windows or frames), or that your .js file is not being loaded. If you use the steps and debugging tools listed at <a href="https://developer.mozilla.org/en/Building_an_Extension" rel="nofollow">https://Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-48325874883560630362009-07-15T23:22:58.782-05:002009-07-15T23:22:58.782-05:00Hey Mark,
Thanks for the article!
I am currentl...Hey Mark,<br /><br />Thanks for the article! <br /><br />I am currently re-writing an extension of mine to have it moved from the sandbox. I had a couple of functions in my JS file doing certain tasks and now I tried doing the following:<br /><br />if(!com) var com={};<br />if(!com.hottypo) com.hottypo={};<br />if(!com.hottypo.myPackage) com.hottypo.myPackage={};<br /><br />com.hottypo.myPackage Unknownhttps://www.blogger.com/profile/09565308071521818623noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-89344797810131911652009-06-28T12:44:04.370-05:002009-06-28T12:44:04.370-05:00Jaap - have you tried removing "this.", ...Jaap - have you tried removing "this.", and just referencing "_member_variable"?<br /><br />If there is a naming-conflict with another variable in a function closer in the scope chain, you could access the inner variable as fully-qualified, e.g. org.mozdev.dictionarysearch._member_variable.<br /><br />I'd also recommend writing the object as a function with a closure, Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-45324750995377583722009-06-28T12:09:37.829-05:002009-06-28T12:09:37.829-05:00Hi,
I got an email from mozilla addons that I nee...Hi,<br /><br />I got an email from mozilla addons that I need to wrap my variables and functions in the way you suggest. However it does not seem to work because the "this" variable is not pointing to the object I defined but to a chrome window. See the example below<br /><br />What should I do to correct this?<br /><br />function debug (str) {<br /> consoleService = Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-16857766543900322702009-04-28T14:06:00.000-05:002009-04-28T14:06:00.000-05:00I wrote an article regarding the importance of nam...I wrote an article regarding the <A HREF="http://www.louddog.com/bloggity/2009/04/the-importance-of-namespace.php" REL="nofollow">importance of namespacing</A> in JavaScript.Matt DeClairehttp://www.louddog.comnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-68844126030203348202009-04-16T11:53:00.000-05:002009-04-16T11:53:00.000-05:00Thanks, Mark. I believe I understand now. We can ...Thanks, Mark. I believe I understand now. We can set up the base of the namespace in file1 and then extend the namespace in file2 or even add more functions to the original namespace. I like this method. Great job. Thanks for your assistance.Vinny2020https://www.blogger.com/profile/05644045433649933896noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-57181657805790885662009-04-16T10:43:00.000-05:002009-04-16T10:43:00.000-05:00Vinny2020 - First, we should use something like my...Vinny2020 - First, we should use something like my <A HREF="/2008/05/javascript-namespace-function.html" REL="nofollow">JavaScript namespace function</A> to declare the namespaces so that each level is only added when necessary, without overwriting anything.<br /><br />Using this, assume in the first file:<br /><br />"com.example".namespace().a = function(){...};<br />"com.example&Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-61520121992001516782009-04-16T09:56:00.000-05:002009-04-16T09:56:00.000-05:00Mark, yes that will work, but in the additional ja...Mark, yes that will work, but in the additional javascript files how would I define the function? Would I have to use the whole package name? If I redefine the initial "package" function in file 2, that will blow away anything created in file 1. So after the initial definition in file 1, do I then use the full "namespace" to define functions in file 2"?<br /><br />Using your example, in file 2 Vinny2020https://www.blogger.com/profile/05644045433649933896noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-12500233037448903292009-04-15T18:49:00.000-05:002009-04-15T18:49:00.000-05:00Vinny2020 - it really doesn't matter. Whether...Vinny2020 - it really doesn't matter. Whether it is a "sub-domain" or a function within a "domain", they are both accessed through the same dot-notation. So you could start out by declaring one "package" with various sub-packages and functions in your first source file. In each additional source file, you could add additional sub-packages and/or functions.Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-9907598282073813512009-04-15T13:48:00.000-05:002009-04-15T13:48:00.000-05:00I like this technique. However I am wondering, ho...I like this technique. However I am wondering, how would you use your method to encapsulate 3 javascript source files, but still have them as the same "package"?<br /><br />I'm sure I could do it by adding another "sub domain" to the dot-notation, but what if I want them all to have the same "domain"?Vinny2020https://www.blogger.com/profile/05644045433649933896noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-60394943460128227492009-03-27T08:24:00.000-05:002009-03-27T08:24:00.000-05:00Anonymous - I guess that is why I didn't recommend...Anonymous - I guess that is why I didn't recommend it. I'm removing it from the post as it is incorrect.Mark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-62109297230930887132009-03-26T22:26:00.000-05:002009-03-26T22:26:00.000-05:00The code you provided:if(!com) var com={};if(!com....The code you provided:<BR/><BR/>if(!com) var com={};<BR/>if(!com.ziesemer) com.ziesemer={};<BR/>if(!com.ziesemer.myPackage) com.ziesemer.myPackage={};<BR/><BR/>with(com.ziesemer.myPackage){<BR/> name = "visitor";<BR/> greeting = "hello " + name;<BR/> alertHello = function(){<BR/> alert(greeting);<BR/> }<BR/>}<BR/><BR/>com.ziesemer.myPackage.alertHello();<BR/><BR/>throws an error when Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-17429611499269360572009-01-24T15:04:00.000-06:002009-01-24T15:04:00.000-06:00Ryan - I don't know, especially without seeing...Ryan - I don't know, especially without seeing more details. Just check for typos, etc. The code is definitely correct.<BR/><BR/>Claudijo - I'd have to say that this and the extensions windows are two different issues. The main goal behind this post is simply managing the namespace of a given scope. Your issue with the different extension windows is working across different scopes.<BRMark A. Ziesemerhttps://www.blogger.com/profile/12752422620777325409noreply@blogger.comtag:blogger.com,1999:blog-4222197394079314848.post-59443488674179549502009-01-24T12:24:00.000-06:002009-01-24T12:24:00.000-06:00Thanks for a good reference. As a novice javascrip...Thanks for a good reference. <BR/><BR/>As a novice javascript programmer and extension developer I have a question. How is the problem with loose variables exploited (or even used as a positive side effect)? <BR/><BR/>As far as I know, It is not sufficient to make a variabel global in the overlay file in order to access it from a javascript loaded in the firefox sidebar (instead I have to do Anonymousnoreply@blogger.com