extend.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var mix_1 = require("./mix");
  4. var is_function_1 = require("./is-function");
  5. var extend = function (subclass, superclass, overrides, staticOverrides) {
  6. // 如果只提供父类构造函数,则自动生成子类构造函数
  7. if (!is_function_1.default(superclass)) {
  8. overrides = superclass;
  9. superclass = subclass;
  10. subclass = function () { };
  11. }
  12. var create = Object.create ?
  13. function (proto, c) {
  14. return Object.create(proto, {
  15. constructor: {
  16. value: c
  17. }
  18. });
  19. } :
  20. function (proto, c) {
  21. function Tmp() { }
  22. Tmp.prototype = proto;
  23. var o = new Tmp();
  24. o.constructor = c;
  25. return o;
  26. };
  27. var superObj = create(superclass.prototype, subclass); // new superclass(),//实例化父类作为子类的prototype
  28. subclass.prototype = mix_1.default(superObj, subclass.prototype); // 指定子类的prototype
  29. subclass.superclass = create(superclass.prototype, superclass);
  30. mix_1.default(superObj, overrides);
  31. mix_1.default(subclass, staticOverrides);
  32. return subclass;
  33. };
  34. exports.default = extend;
  35. //# sourceMappingURL=extend.js.map