Two Laptop Bag*
The moose likes HTML, CSS and JavaScript and the fly likes managing the global space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "managing the global space" Watch "managing the global space" New topic
Author

managing the global space

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

I'm doing some reading, trying to upgrade my JS skills. I'm trying to understand the global namespace. It's typical for my external js files to start like this:


etc...Basically, caching the selectors to minimize DOM traversing. If I understand correctly, this is a recipe for disaster and should be done more like this:


...with the variables accessed using dot or bracket notation. Am I understanding this correctly or missing an important concept?

Also, how should functions be managed? For instance, this same script has this code:


Does this result in a global/public function? Should it be added to the headlineNS object as a method? Does it make any difference if such functions are declared within or outside of $(document).ready(function($)? So far it's been my habit to put everything inside $(document).ready(function($) but I'm not sure that's the best practice.




"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

vars declared in the ready function are scoped to that function, so they won't be available anywhere else.

I'm not a fan of the global pre-fetching approach in the first place. Pre-fetching all the wrapped sets doesn't make much sense to me. It doesn't save any time -- the DOM traversing has to happen in any case -- and performs a bunch of work up front that may or may not end up being used.

I create the wrapped sets when, and if, they are needed, and cache them at that point only if I determine that I'll need them again for sure.

I'd only entertain some other approach if, and only if, there are demonstrable and observable performance issues with grabbing the wrapped sets on the fly. Anything else smacks of premature optimization.

With regards to the function, the only real difference between declaring it in the global scope, versus making it a method of a global object, is what the function context will be when the function is invoked. If that's not important, then there's no compelling reason to unnecessarily make it a method.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

I don't write the pre-fetching until the script is working. I then look to see if there are wrapped sets that I've selected more than twice. That's my general rule of thumb. If I'm only hitting it once or twice then there is probably nothing to be gained by caching. If I'm selecting a wrapped set several times then I'll go ahead and cache it.

Thanks for clarifying the function scope. I'm slowly starting to understand. Now if I could just make sense out of closures....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

What you need is chapters 3 through 6 of Secrets of the JavaScript Ninja . We wrote those to specifically make sure that people really understand functions, scope, closures, and prototypes.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 963
    
  13

Bear Bibeault wrote:What you need is chapters 3 through 6 of Secrets of the JavaScript Ninja . We wrote those to specifically make sure that people really understand functions, scope, closures, and prototypes.

I'm anxiously waiting for it to hit the shelves. The only reason I haven't ordered it already is because I want to make sure I get the latest edition.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

Well, if you order the MEAP, you always get the updates.

The book is in typesetting, so the content is done! Woohoo!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: managing the global space