我試圖在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