04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20: Best Practices<br />

Many JavaScript libraries now subscribe to this theory of development, allowing them to grow and<br />

adapt even as browsers continually change.<br />

Avoid Globals<br />

Closely related to respecting object ownership is avoiding global variables and functions whenever<br />

possible. Once again, this has to do with creating a consistent and maintainable environment in which<br />

scripts will be executed. At most, a single global variable should be created on which other objects and<br />

functions exist. Consider the following:<br />

//two globals - AVOID!!!<br />

var name = “Nicholas”;<br />

function sayName(){<br />

alert(name);<br />

}<br />

This code contains two globals: the variable name and the function sayName() . These can easily be<br />

created on an object that contains both, as in this example:<br />

//one global - preferred<br />

var MyApplication = {<br />

name: “Nicholas”,<br />

sayName: function(){<br />

alert(this.name);<br />

}<br />

};<br />

This rewritten version of the code introduces a single global object, MyApplication , onto which both<br />

name and sayName() are attached. Doing so clears up a couple of issues that existed in the previous<br />

code. First, the variable name overwrites the window.name property, which possibly interferes with other<br />

functionality. Second, it helps to clear up confusion over where the functionality lives. Calling<br />

MyApplication.sayName() is a logical hint that any issues with the code can be identified by looking<br />

at the code in which MyApplication is defined.<br />

An extension of the single global approach is the concept of namespacing, popularized by the Yahoo!<br />

User Interface (YUI) library. Namespacing involves creating an object to hold functionality. In the 2.x<br />

version of YUI, there were several namespaces onto which functionality was attached. Here are some<br />

examples:<br />

❑<br />

❑<br />

❑<br />

YAHOO.util.Dom — Methods for manipulating the DOM<br />

YAHOO.util.Event — Methods for interacting with events<br />

YAHOO.lang — Methods for helping with low - level language features<br />

For YUI, the single global object YAHOO serves as a container onto which other objects are defined.<br />

Whenever objects are used simply to group together functionality in this manner, they are called<br />

namespaces . The entire YUI library is built on this concept, allowing it to coexist on the same page with<br />

any other JavaScript library.<br />

The important part of namespacing is to decide on a global object name that everyone agrees to use and<br />

that is unique enough that others aren ’ t likely to use it as well. In most cases, this can be the name of the<br />

644

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!