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 6: Object-Oriented Programming<br />

(continued)<br />

SubType.prototype = new SuperType();<br />

//first call to SuperType()<br />

SubType.prototype.sayAge = function(){<br />

alert(this.age);<br />

};<br />

The highlighted lines of code indicate when SuperType constructor is executed. When this code is<br />

executed, SubType.prototype ends up with two properties: name and colors . These are instance<br />

properties for SuperType, but they are now on the SubType ’ s prototype. When the SubType constructor<br />

is called, the SuperType constructor is also called, which creates instance properties name and colors<br />

on the new object that mask the properties on the prototype. Figure 6 - 6 illustrates this process.<br />

Initially<br />

SubType<br />

prototype<br />

SubType Prototype<br />

constructor<br />

SubType.prototype new SuperType()<br />

SubType<br />

prototype<br />

SuperType Prototype<br />

constructor<br />

SubType<br />

prototype<br />

SubType Prototype<br />

__proto__<br />

constructor<br />

name (undefined)<br />

colors (array)<br />

var instance new SubType("Nicholas", 29)<br />

SuperType<br />

prototype<br />

SuperType Prototype<br />

constructor<br />

SubType<br />

prototype<br />

instance<br />

__proto__<br />

name "Nicholas"<br />

colors (array)<br />

age 29<br />

Figure 6 - 6<br />

SubType Prototype<br />

__proto__<br />

constructor<br />

name (undefined)<br />

colors (array)<br />

180

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

Saved successfully!

Ooh no, something went wrong!