IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User

Welcome to IWETHEY!

New thanks. one question tho
why should I care about putting a Javascript function within a namespace?

Satan (impatiently) to Newcomer: The trouble with you Chicago people is, that you think you are the best people down here; whereas you are merely the most numerous.
- - - Mark Twain “Pudd’nhead Wilson’s New Calendar,” 1897
New scope and execution...
If you don't it is included in "everything"... and makes it slower in execution and hog memory.

You really need to instrument your stuff to see the difference, but if you have multiples... it'll compound the execution time.
PGP key 1024D/B524687C 2003-08-05
Fingerprint: E1D3 E3D7 5850 957E FED0 2B3A ED66 6971 B524 687C
New It makes for tidier code.
Namespacing it means you use more concise names without risking a collision. It also makes it much easier to create private variables and functions for your code.

Call it defensive programming.

Just Add Story http://justaddstory.wordpress.com/
New Avoids name collisions.
If you have a global function named foo(), and a 3rd party library or someone else in your organization comes along and defines foo() as well, There Can Be Only One and the first defined will lose.

As Wade mentioned, it's also a tidiness thing. Everything in one place can make for a giant morass of code to sort through. Take a traipse through the Window object (where everything in global scope is put) some time to see all the stuff already in there.

While execution speed might become significant with hundreds of thousands of global definitions, I suspect it's not worth worrying about otherwise. Names are hashed into scopes so the lookup is O(1), and nested namespaces will probably result in more memory usage overall depending on what the default Javascript hashmap internal size is and how the hashmaps are grown. A short test with the Chrome profiler shows that this is indeed the case: an Object with 100,000 items in it takes up 20% less memory that 100 Objects with 1000 items each. In either case the memory overhead is negligible at reasonable numbers of items, as you would see in any but the most horrifically large JavaScript applications.
Welcome to Rivendell, Mr. Anderson.
     Javascript sucks today - (lincoln) - (9)
         From StackOverflow - (drook)
         This: - (malraux) - (1)
             Have I mentioned lately ... - (drook)
         forgot to add - (lincoln) - (5)
             That's what this is for: - (malraux) - (4)
                 thanks. one question tho - (lincoln) - (3)
                     scope and execution... - (folkert)
                     It makes for tidier code. - (static)
                     Avoids name collisions. - (malraux)

Red Rover, Red Rover, Bob Lazar's comin' over.
79 ms