18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

Scope<br />

Programmers in any language understand the concept of scope, meaning the area in which certain variables<br />

are accessible.<br />

Public, protected, and private<br />

In traditional object-oriented programming, a lot of focus is placed on the public and private scopes. An<br />

object’s properties in the public scope can be accessed from outside the object, meaning that after a developer<br />

creates an instance of the object, that property can be used. Properties in the private scope, however,<br />

can only be accessed from within the object itself, meaning that these properties don’t exist to the outside<br />

world. This also means that subclasses of the class defining the private properties and methods<br />

can’t access them either.<br />

More recently, another scope has become popular: protected. Although different languages have different<br />

rules for the protected scope, it generally is used to define properties and methods that act private except<br />

that they are accessible by subclasses.<br />

The discussion of these scopes in reference to ECMAScript is almost a moot point because only one<br />

scope of these three exists: the public scope. All properties and methods of all objects in ECMAScript are<br />

public. You must take great care, therefore, when defining your own classes and objects. Keep in mind<br />

that all properties and methods are public by default.<br />

This problem has been tackled by many developers online trying to come up with effective property<br />

scoping schemes. Due to the lack of a private scope, a convention was developed to indicate which properties<br />

and methods should be considered private. This convention involves adding two underscores<br />

before and after the actual property name. <strong>For</strong> example:<br />

obj.__color__ = “red”;<br />

In this code, the color property is intended to be private. Remember, adding these underscores doesn’t<br />

change the fact that the property is public; it just indicates to other developers that it should be considered<br />

private.<br />

Some developers also prefer to use a single underscore to indicate private members, such as<br />

obj._color.<br />

Static is not static<br />

88<br />

The static scope defines properties and methods accessible all the time from one location. In Java, classes<br />

can have static properties and methods that are accessible without instantiating an object of that class,<br />

such as java.net.URLEncoder, whose function encode() is a static method.<br />

Strictly speaking, ECMAScript doesn’t have a static scope. It can, however, provide properties and methods<br />

on constructors. Remember, constructors are just functions. Functions are objects, and objects can<br />

have properties and methods. <strong>For</strong> instance:<br />

function sayHi() {<br />

alert(“hi”);<br />

}

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

Saved successfully!

Ooh no, something went wrong!