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.

In addition to instance properties and properties of prototypes, <strong>JavaScript</strong> allows you to define<br />

class properties (also known as static properties), properties of the type rather than of a<br />

particular object instance. An example of a class property is Number.MAX_VALUE. This<br />

property is a type-wide constant, and therefore is more logically located in the class<br />

(constructor) rather than individual Number objects. But how are class properties<br />

implemented?<br />

Because constructors are functions and functions are objects, you can add properties to<br />

constructors. Class properties are added this way. Though technically doing so adds an<br />

instance property to a type‘s constructor, we‘ll still call it a class variable. Continuing our<br />

example,<br />

Robot.isMetallic = true;<br />

defines a class property of the Robot object by adding an instance variable to the constructor.<br />

It is important to remember that static properties exist in only one place, as members of<br />

constructors. <strong>The</strong>y are therefore accessed through the constructor rather than an instance of<br />

the object.<br />

As previously explained, static properties typically hold data or code that does not depend on<br />

the contents of any particular instance. <strong>The</strong> toLowerCase() method of the String object could<br />

not be a static method because the string it returns depends on the object on which it was<br />

invoked. On the other hand, the PI property of the Math object (Math.PI) and the parse()<br />

method of the String object (String.parse()) are perfect candidates, because they do not<br />

depend on the value of any particular instance. You can see from the way they are accessed<br />

that they are, in fact, static properties. <strong>The</strong> isMetallic property we just defined is accessed<br />

similarly, as Robot.isMetallic.<br />

Inheritance via the Prototype Chain<br />

Inheritance in <strong>JavaScript</strong> is achieved through prototypes. It is clear that instances of a particular<br />

object ―inherit‖ the code and data present in the constructor‘s prototype. But what we haven‘t<br />

really seen so far is that it is also possible to derive a new object type from a type that already<br />

exists. Instances of the new type inherit all the properties of their own type in addition to any<br />

properties embodied in their parent.<br />

As an example, we can define a new object type that inherits all the capabilities of our Robot<br />

object by ―chaining‖ prototypes:<br />

function UltraRobot(extraFeature)<br />

{<br />

}<br />

if (extraFeature)<br />

this.feature = extraFeature;<br />

UltraRobot.prototype = new Robot();<br />

UltraRobot.prototype.feature = "Radar";<br />

<strong>The</strong> only new concept in this example is setting UltraRobot‘s prototype to a new instance of a<br />

Robot object. Because of the way properties are resolved via prototypes, UltraRobot objects<br />

―contain‖ the properties of the UltraRobot object as well as those of Robot:<br />

var guard = new UltraRobot("Performs Calculus");

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

Saved successfully!

Ooh no, something went wrong!