天天看點

學習筆記-------JavaScript的繼承

//原型
function fruit(name){
    this.name = name;
}
//原型的方法
fruit.prototype.say = function(type){ return "I am " + this.name +", and I am " + type + "!!" };
//子類繼承原型
function apple(name,type){
    //通過CALL方法繼承父類的name屬性。
    fruit.call(this,name);
    //子類自有屬性
    this.type = type;
}
//設定原型,繼承原型的方法
apple.prototype = new fruit();
// 設定原型的構造器 
apple.prototype.constructor = apple;
//測試代碼
alert(new apple("apple","sweet").say(new apple("apple","sweet").type));
           

繼續閱讀