Javascript的简单继承

function First(age, name) {

this.age = age;

this.name = name;

}

First.prototype.getAge = function() {

return this.age;

};

First.prototype.toString = function() {

return "[ age: " + this.age + ", name: " + this.name + " ]";

};

function Second(age, name, password) {

First.call(this, age, name);

this.password = password;

}

Second.prototype = new First();

Second.prototype.constructor = Second;

Second.prototype.getPassword = function() {

return this.password;

};

Second.prototype.toString = function() {

return "[ age: " + this.age + ", name: " + this.name + ", password: " + this.password + " ]";

};

//由于通过Second.prototype = new First();

//继承了父类First的name和age属性,需要通过子类显示删除两个属性

delete Second.prototype.age;

delete Second.prototype.name;

//继承First

function Second2(age, name, sex) {

First.call(this, age, name);

this.sex = sex;

}

Second2.prototype.toString = function() {

return "[ age: " + this.age + ", name: " + this.name + ", sex: " + this.sex + " ]";

};

Second2.prototype.getSex = function() {

return this.sex;

};

function Third(age, name, password, address, email, sex) {

Second.call(this, age, name, password);

Second2.call(this, age, name, sex);

this.address = address;

this.email = email;

}

Third.prototype = new Second();

Third.prototype.constructor = Third;

Third.prototype.getAddress = function() {

return this.address;

};

Third.prototype.setEmail = function(email) {

this.email = email;

};

Third.prototype.toString = function() {

return "[ age: " + this.age + ", name: " + this.name + ", password: " + this.password + ", address: " + this.address + ", email: " + this.email + " ]";

};

delete Third.prototype.name;

delete Third.prototype.age;

delete Third.prototype.password;