kaixuan1115 / notes

笔记收录
6 stars 0 forks source link

Nodejs继承简单实现 #16

Closed xiaokaixuan closed 5 years ago

xiaokaixuan commented 5 years ago
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);