| Author |
managing the global space
|
J. Kevin Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 380
|
|
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.
|
"I have a mind like a steel... uh... thingy."
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56157
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
J. Kevin Robbins
Ranch Hand
Joined: Dec 16, 2010
Posts: 380
|
|
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: 56157
|
|
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
Ranch Hand
Joined: Dec 16, 2010
Posts: 380
|
|
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: 56157
|
|
Well, if you order the MEAP, you always get the updates.
The book is in typesetting, so the content is done! Woohoo!
|
 |
 |
|
|
subject: managing the global space
|
|
|