Nodejs继承简单实现
代码如下:
function Animal() {
this.name = 'animal';
}
function Cat(name) {
Animal.call(this);
this.name = name || 'Tom';
}
(function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
})(Cat, Animal);
或
function Animal() {
this.name = 'animal';
}
function Cat(name) {
Animal.call(this);
this.name = name || 'Tom';
}
require('util').inherits(Cat, Animal);
// <=> Cat.super_ = Animal; Object.setPrototypeOf(Cat.prototype, Animal.prototype);

浙公网安备 33010602011771号