The problem is that whichever library you include
last will overwrite the previous. Therefore, any code depending on the first will end
up breaking. jQuery has an option that enables you to remap the $() to avoid this
problem. If you think this isn??™t a very common situation, you??™d be surprised. As of this
writing, the popular site Digg.com is actually in the process of switching from Prototype
to jQuery, so it has to be careful with code collisions during the transition.
??? It can make your code easier to read. By encapsulating things into a single object, it
establishes code ownership and enables the code within to be more self-contained.
For example, any of the code on my site is in a SNOOK namespace, making it clear that
everything is related. Likewise, the Yahoo! User Interface library (which will be covered
in Chapter 4) uses YAHOO as its namespace. There??™s no confusion. In addition, I can
store shared variables within the namespace that all functions within can have access
to without worrying about conflicting with other scripts.
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 68
But is using namespaces necessary? Certainly not. People have lived without namespaces
for the past ten years of JavaScript development. However, it doesn??™t hurt to keep things clean
and organized from the beginning.
Here is some code from my site to demonstrate how I declared a namespace. In my blog
I have a comment form that enables people to leave feedback about a particular post.
Pages:
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125