wuxianqiang / blog

博客系列
17 stars 4 forks source link

实现一个函数A继承B #275

Open wuxianqiang opened 4 years ago

wuxianqiang commented 4 years ago

node中inherits实现原理

function inherits(ctor, superCtor) {
  ctor.super_ = superCtor;
  Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
}
const util = require('util');
const EventEmitter = require('events');

function MyStream() {
  EventEmitter.call(this);
}

util.inherits(MyStream, EventEmitter);