function Father(name) { this.name = name ; this.getName = function ()
{ return this.name;
}
} function Son(name,age) { this._newMethod = Father; this._newMethod(name);
delete this._newMethod;
this.age = age; this.getAge = function ()
{ return this.age;
}
} var father = new Father("Tom"); var son = new Son("Jack",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类getName()方法 console.log(son.getAge());//18
多继承(利用对象冒充可以实现多继承)
function FatherA(name) { this.name = name ; this.getName = function ()
{ return this.name;
}
} function FatherB(job) { this.job = job; this.getJob = function ()
{ return this.job;
}
} function Son(name,job,age) { this._newMethod = FatherA; this._newMethod(name);
delete this._newMethod; this._newMethod = FatherB; this._newMethod(job);
delete this._newMethod;
this.age = age; this.getAge = function ()
{ return this.age;
}
} var fatherA = new FatherA("Tom"); var fatherB = new FatherB("Engineer"); var son = new Son("Jack","Programmer",18); console.log(fatherA.getName());//Tom console.log(fatherB.getJob());//Engineer console.log(son.getName());//Jack//继承父类FatherA的getName()方法 console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法 console.log(son.getAge());//18
call()方法
function Father(name) { this.name = name ; this.getName = function ()
{ return this.name;
}
} function Son(name,job,age) { Father.call(this,name);
this.age = age; this.getAge = function ()
{ return this.age;
}
} var father = new Father("Tom"); var son = new Son("Jack","Programmer",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类getName()方法 console.log(son.getAge());//18
多继承(利用call()方法实现多继承)
function FatherA(name) { this.name = name ; this.getName = function ()
{ return this.name;
}
} function FatherB(job) { this.job = job; this.getJob = function ()
{ return this.job;
}
} function Son(name,job,age) { FatherA.call(this,name); FatherB.call(this,job);
this.age = age; this.getAge = function ()
{ return this.age;
}
} var fatherA = new FatherA("Tom"); var fatherB = new FatherB("Engineer"); var son = new Son("Jack","Programmer",18); console.log(fatherA.getName());//Tom console.log(fatherB.getJob());//Engineer console.log(son.getName());//Jack//继承父类FatherA的getName()方法 console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法 console.log(son.getAge());//18
apply()方法
function Father(name) { this.name = name ; this.getName = function ()
{ return this.name;
}
} function Son(name,job,age) { Father.apply(this,new Array(name));
this.age = age; this.getAge = function ()
{ return this.age;
}
} var father = new Father("Tom"); var son = new Son("Jack","Programmer",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类getName()方法 console.log(son.getAge());//18
多继承(利用apply()方法实现多继承)
function FatherA(name) { this.name = name ; this.getName = function ()
{ return this.name;
}
} function FatherB(job) { this.job = job; this.getJob = function ()
{ return this.job;
}
} function Son(name,job,age) { FatherA.apply(this,new Array(name)); FatherB.apply(this,new
Array(job));
this.age = age; this.getAge = function ()
{ return this.age;
}
} var fatherA = new FatherA("Tom"); var fatherB = new FatherB("Engineer"); var son = new Son("Jack","Programmer",18); console.log(fatherA.getName());//Tom console.log(fatherB.getJob());//Engineer console.log(son.getName());//Jack//继承父类FatherA的getName()方法 console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法 console.log(son.getAge());//18
原型链方法
function Father() {
} Father.prototype.name = "Tom"; Father.prototype.getName = function () { return this.name;
}; function Son() {
} Son.prototype
= new Father(); Son.prototype.age = 18; Son.prototype.getAge = function () { return this.age;
}; var father = new Father(); var son = new Son(); console.log(father.getName());//Tom console.log(son.getName());//Tom//继承父类FatherA的getName()方法 console.log(son.getAge());//18
混合方式(call()+原型链)
function Father(name) { this.name = name;
} Father.prototype.getName = function () { return this.name;
}; function Son(name,age) { Father.call(this,name); this.age = age;
} Son.prototype
= new Father(); Son.prototype.getAge = function () { return this.age;
}; var father = new Father("Tom"); var son = new Son("Jack",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类Father的getName()方法 console.log(son.getAge());//18
多态机制实现
function Person(name) { this.name = name; if (typeof this.getName
!== "function"){ Person.prototype.getName
= function () { return this.name;
}
} if (typeof this.toEat
!== "function"){ Person.prototype.toEat
= function (animal) { console.log( this.getName()
+ "说去吃饭:");
animal.eat();
}
}
} function Animal(name) { this.name = name; if (typeof this.getName
!== "function"){ Animal.prototype.getName
= function () { return this.name;
}
}
} function Cat(name) { Animal.call(this,name); if (typeof this.eat
!== "function"){ Cat.prototype.eat
= function () { console.log(this.getName()
+ "吃鱼");
}
}
} Cat.prototype = new Animal(); function Dog(name) { Animal.call(this,name); if (typeof this.eat
!== "function"){ Dog.prototype.eat
= function () { console.log(this.getName()
+ "啃骨头");
}
}
} Dog.prototype = new Animal();
var person = new Person("Tom"); person.toEat(new Cat("cat"));//Tom说去吃饭:cat吃鱼 person.toEat(new Dog("dog"));//Tom说去吃饭:dog啃骨头