1//第一种方法是最省事,但会让子类的constructor变成父类的constructor
function people() {
this.name = 'kay';
}
people.prototype.getName = function(){ return this.name }
function man() {
this.sex = 'male';
people.call(this);
}
man.prototype = people.prototype;
var MAN = new man(); console.log(MAN.getName());// 'kay'
2//第二种方法是最正确的
function people() {
this.name = 'snowin';
}
people.prototype.getName = function() { return this.name };
function man() {
this.sex = 'male';
people.call(this);
}
man.prototype = new people();
man.prototype.constructor = man;
var MAN = new man();
console.log(MAN.getName());//'snowin'
网友评论