我试图在 javascript 中学习 OO 技术。大多数网站都使用原型继承。
但是,我试图理解为什么以下内容不好(并且仍然可以实现原型继承可以做的事情):
//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", "a@a.com"); //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();