Main Page

new properties

All new properties and methods must be added after the line that deletes the new method. Otherwise,
you run the risk of overwriting the new properties and methods with those of the superclass:
function ClassB(sColor, sName) {
this.newMethod = ClassA;
delete this.newMethod; = sName;
this.sayName = function () {
To prove that this works, you can run the following example:
var objA = new ClassA(“red”);
var objB = new ClassB(“blue”, “Nicholas”);
objA.sayColor(); //outputs “red”
objB.sayColor(); //outputs “blue”
objB.sayName(); //outputs “Nicholas”
As an interesting side note, object masquerading supports
multiple inheritance
, meaning that a class can
inherit from multiple superclasses. Multiple inheritance is represented in UML by showing the previous
superclasses of the subclass as shown in Figure 4-2.
Figure 4-2
For example, if two classes, ClassX and Class Y, exist, and ClassZ wishes to inherit from both, then the
following code can be used:
function ClassZ() {
this.newMethod = ClassX;
delete this.newMethod;
this.newMethod = ClassY;
Chapter 4
07_579088 ch04.qxd 3/28/05 11:36 AM Page 106

JavaScript EditorFree JavaScript Editor     Ajax Editor