如何让子类继承父类的属性
2017-10-24 本文已影响0人
胖太_91bf
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'