有关javascript的继承在《javascript 高级程序设计》第三版 中讲的很详细,也极力推荐初学者学习本书。
javascript
function object(o) { function F(){} F.prototype = o; return new F(); } function inherit(subType,superType) { var p = object(superType.prototype); p.constructor = subType; subType.prototype = p; } function Super(name) { this.name = name; this.colors = ['red','blue']; } Super.prototype.getName = function() { console.log('This is name ' + this.name); } function Sub (name ,age) { Super.call (this,name); this.age = age; } inherit(Sub,Super);//Node.js的高阶函数继承使用的这种方式 Sub.prototype.getAge = function() { console.log('This is age ' + this.age ); } var inst = new Sub('Hello,world',20); inst.colors.push('white'); console.log(inst.colors); inst.getName(); inst.getAge(); var inst1 = new Sub('Hi,god',30); inst1.colors.push('yellow'); console.log(inst1.colors); inst1.getName(); inst1.getAge();