javascript 经典的继承方式

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

javascript 经典的继承方式

上一篇:CentOS安装NodeJS v0.10.25 + Express


下一篇:Netsharp快速入门 介绍及需求说明