我试图在JavaScript中学习OO技术。大多数网站使用原型继承。为什么在javascript中继承的原型是必需的
但是,我想了解为什么以下是坏的(现在仍然能达到什么原型继承可以做):
//create parent class
var Person = function (vId, vName) {
this.Id = vId;
this.Name = vName;
this.Display = function() {
alert("Id=" + this.Id);
}
};
//create new class
var Emp = function (vId, vName, vOfficeMail) {
Person.call(this, vId, vName)
this.OfficeEmail = vOfficeMail;
this.Display = function() {
alert("Id=" + this.Id + ", OfficeMail=" + this.OfficeEmail);
}
};
//create instance of child class
var oEmp = new Emp(1001, "Scott", "[email protected]"); //using Child's constructor
//call display method in child class
oEmp.Display();
//create instance of parent class
var oPerson = new Person(1002, "Smith"); //using Parent's constructor
//call display method in parent class
oPerson.Display();
[*原型继承*](http://en.wikipedia.org/wiki/Prototype-based_programming )是JavaScript知道的唯一继承。 – Bergi 2012-07-08 20:21:50