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();

 

posted on 2014-06-01 14:35  c3tc3tc3t  阅读(169)  评论(0编辑  收藏  举报