# JavaScript 继承

# 构造函数、原型对象和实例之间的关系

function F() {}
let f = new F();


//构造器
F.prototype.constructor === F;
F.__proto__ === Function.prototype;  // <== 注意此条 不好理解
Function.prototype.__proto__ === Object.prototype;
Object.prototype.__proto__ === null;

//实例
f.__proto__ === F.prototype;
F.prototype.__proto__ === Object.prototype;
Object.prototype.__proto__ === null;

# ES6

function _inherits(subClass, superClass) {
  if (typeof superClass !== "function" && superClass !== null) {
    throw new TypeError("Super expression must either be null or a function");
  }
  subClass.prototype = Object.create(superClass && superClass.prototype, {
    constructor: { value: subClass, writable: true, configurable: true }
  });
  if (superClass) _setPrototypeOf(subClass, superClass);
}
陕ICP备20004732号-3