javascript 经典的继承方式

 1 function inherits(subClass, supClass){
 2     function temp(){};
 3     temp.prototype = supClass.prototype;
 4     subClass.prototype = new temp();
 5     subClass.prototype.constructor = subClass;
 6 };
 7 
 8 function Supper(name) {
 9     this.name = name || 'Apple';
10 };
11 
12 Supper.prototype.eat = function(food) {
13     console.log('today ' + this.name + ' eat ' + food);
14 };
15 
16 function Sub(name) {
17     this.name = name || 'Google';
18 };
19 
20 inherits(Sub, Supper);
21 
22 var test = new Sub();
23 test.eat('eag'); // today Google eat eag
View Code

子类的构造函数也可以这么写

function Sub(name) {
    Supper.call(this, name);
};

 

posted on 2014-02-15 16:49  罪恶  阅读(168)  评论(0编辑  收藏  举报

导航