当前位置: 代码迷 >> JavaScript >> javascript 承袭
  详细解决方案

javascript 承袭

热度:373   发布时间:2012-09-03 09:48:39.0
javascript 继承
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype); //create object
prototype.constructor = subType; //augment object
subType.prototype = prototype; //assign object
}

function object(o){
function F(){}
F.prototype = o;
return new F();
}

function SuperType(name){
this.name = name;
this.colors = [“red”, “blue”, “green”];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
inheritPrototype(SubType, SuperType);
SubType.prototype.sayAge = function(){
alert(this.age);
};
  相关解决方案