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
子类的构造函数也可以这么写
function Sub(name) { Supper.call(this, name); };