11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

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.

object: because these are exactly the properties and methods of a generic Object!<br />

Now that we‘ve explained the theoretical basis for <strong>JavaScript</strong>‘s object-oriented features, let‘s<br />

see how it translates into implementation. If you‘re feeling a bit lost at this point, that‘s okay;<br />

we‘ll reiterate the theory as we cover the concrete details.<br />

Constructors<br />

Object instances are created with constructors, which are basically special functions that<br />

prepare new instances of an object for use. Every constructor contains an object prototype that<br />

defines the code and data that each object instance has by default.<br />

Note Before delving any deeper, some commentary regarding nomenclature is appropriate.<br />

Because everything in <strong>JavaScript</strong> except primitive data and language constructs is an<br />

object, the term ―object‖ is used quite often. It is important to differentiate between a type<br />

of object, for example, the Array or String object, and an instance of an object, for<br />

example, a particular variable containing a reference to an Array or String. A type of<br />

object is defined by a particular constructor. All instances created with that constructor are<br />

said to have the same ―type‖ or ―class‖ (to stretch the definition of class a bit). To keep<br />

things clear, remember that a constructor and its prototype define a type of object, and<br />

objects created with that constructor are instances of that type.<br />

We‘ve seen numerous examples of object creation, for example,<br />

var s = new String();<br />

This line invokes the constructor for String objects, a function named String(). <strong>JavaScript</strong><br />

knows that this function is a constructor because it is called in conjunction with the new<br />

operator.<br />

We can define our own constructor by defining a function:<br />

function Robot()<br />

{<br />

}<br />

This function by itself does absolutely nothing. However, we can invoke it as a constructor just<br />

like we did for String():<br />

var guard = new Robot();<br />

We have now created an instance of the Robot object. Obviously, this object is not particularly<br />

useful. More information about object construction is necessary before we proceed.<br />

Note Constructors don’t have to be named with an initial uppercase. However, doing so is<br />

preferable because it makes the distinction clear between a constructor (initial uppercase)<br />

that defines a type and an instance of a type (initial lowercase).<br />

When a constructor is invoked, the interpreter allocates space for the new object and implicitly<br />

passes the new object to the function. <strong>The</strong> constructor can access the object being created<br />

using this, a special keyword that holds a reference to the new object. <strong>The</strong> reason the<br />

interpreter makes this available is so the constructor can manipulate the object it is creating<br />

easily. For example, it could be used to set a default value, so we can redefine<br />

our constructor to reflect this ability:<br />

function Robot()<br />

{

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

Saved successfully!

Ooh no, something went wrong!