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
子类的构造函数也可以这么写
function Sub(name) { Supper.call(this, name); };
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。