首页 > 代码库 > javascript类继承

javascript类继承

function extend(subClass, superClass) {
    var f = function() {};
    f.prototype = superClass.prototype;
    subClass.prototype = new f();
    subClass.superClass = superClass.prototype;
}

var parent = function (name, age) {
    this._name = name;
    this._age = age;
};

parent.prototype.show = function () {
    console.info(this._name + " : " + this._age);
};


var son = function(name,age,height) {
    this._height  = height;
    son.superClass.constructor.call(this,name,age);
};

extend(son, parent);
//这里没有重写父类也就是parent的show方法
son.prototype.show=function() {

    console.info(this._name + " : " + this._age+" : "+this._height);
};
var s1 = new son("jack",12,120);

s1.show();

var p1 = new parent("father",40);
p1.show();