Too much recursion 

Higher Order Javascript

 



   Subscribe in NewsGator Online    Subscribe in Rojo    Add to Google      Add to My AOL   Subscribe in FeedLounge   Add to netvibes  

Brendan Eich's Roadmap  Standard ECMA-262  www.schillmania.com  www.quirksmode.org 
www.snook.ca  www.davidflanagan.com  www.kryogenix.org  www.thomasfrank.se 
www.dustindiaz.com  Yahoo! UI Library  www.crockford.com  www.brainjacked.com 
chadlindstrom.ca  www.browserland.org     
       

Sunday, July 16, 2006

 

Namespacing and Application Structure #1


I use the YAHOO style architecture; thanks in no small part to Dusin Diaz. But even with his good examples, I still didn't feel at home. Then I came across this code:
__=function typesNamespace(){
window.isBool = function(a){
return typeof a=='boolean';
}}();
// alert(__); = 'undefined'
// alert(typesNamespace) = 'error undefined' ???
Which confused me (not hard). The author emailed me back and explained:

"It is actually the exact same thing as writing:


window.isBool = ....
The types namespace isn't a great example, but the reason I do this is so that I can have private variables that don't pollute the global namespace:


(function whateverNamespace(){
var AAA, BBB;

_w.someFunction = function(){
// do stuff with AAA or BBB
}
})();
This way AAA and BBB don't exist under window[]."-Mark

~~~~~~~~~~~~~~~~

Here is a couple blocks of code which are under the REM namespace, both blocks do the same thing. Looking at the first block tells me what is actually happening.
// block #A
var REM={};
(function(){
REM.b=b='boolean';//b is private
REM.isBool = function(a){
return typeof a==b;
}
})();
alert(REM.isBool + "\n"+ REM.isBool(true) + "\n" + REM.b);
//
// block #B
var REM=function(){
var b='boolean'; //b is private
return { isBool: function(a){
return typeof a==b;
}, b: b // but b is returned here
}
}();
alert(REM.isBool + "\n"+ REM.isBool(true) + "\n" + REM.b);
~~~~~~~~~~~~~~~~

Comments: Post a Comment

Links to this post:

Create a Link



<< Home

Archives

June 2006   July 2006   August 2006   September 2006  

This page is powered by Blogger. Isn't yours?