class2browser.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * 将es6的class语法转为浏览器可以直接使用的语法,
  3. * 取自 https://babeljs.io/repl
  4. * 需要添加 @babel/plugin-transform-classes
  5. * 是要到的方法添加了`c2b`前缀
  6. */
  7. function c2b_classCallCheck(instance, Constructor) {
  8. if (!(instance instanceof Constructor)) {
  9. throw new TypeError("Cannot call a class as a function");
  10. }
  11. }
  12. function _defineProperties(target, props) {
  13. for (var i = 0; i < props.length; i++) {
  14. var descriptor = props[i];
  15. descriptor.enumerable = descriptor.enumerable || false;
  16. descriptor.configurable = true;
  17. if ("value" in descriptor) descriptor.writable = true;
  18. Object.defineProperty(target, descriptor.key, descriptor);
  19. }
  20. }
  21. function c2b_createClass(Constructor, protoProps, staticProps) {
  22. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  23. if (staticProps) _defineProperties(Constructor, staticProps);
  24. return Constructor;
  25. }
  26. function c2b_inherits(subClass, superClass) {
  27. if (typeof superClass !== "function" && superClass !== null) {
  28. throw new TypeError("Super expression must either be null or a function");
  29. }
  30. subClass.prototype = Object.create(superClass && superClass.prototype, {
  31. constructor: { value: subClass, writable: true, configurable: true }
  32. });
  33. if (superClass) _setPrototypeOf(subClass, superClass);
  34. }
  35. function _setPrototypeOf(o, p) {
  36. _setPrototypeOf =
  37. Object.setPrototypeOf ||
  38. function _setPrototypeOf(o, p) {
  39. o.__proto__ = p;
  40. return o;
  41. };
  42. return _setPrototypeOf(o, p);
  43. }
  44. function c2b_createSuper(Derived) {
  45. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  46. return function _createSuperInternal() {
  47. var Super = _getPrototypeOf(Derived),
  48. result;
  49. if (hasNativeReflectConstruct) {
  50. var NewTarget = _getPrototypeOf(this).constructor;
  51. result = Reflect.construct(Super, arguments, NewTarget);
  52. } else {
  53. result = Super.apply(this, arguments);
  54. }
  55. return _possibleConstructorReturn(this, result);
  56. };
  57. }
  58. function _possibleConstructorReturn(self, call) {
  59. if (call && (typeof call === "object" || typeof call === "function")) {
  60. return call;
  61. } else if (call !== void 0) {
  62. throw new TypeError(
  63. "Derived constructors may only return object or undefined"
  64. );
  65. }
  66. return _assertThisInitialized(self);
  67. }
  68. function _assertThisInitialized(self) {
  69. if (self === void 0) {
  70. throw new ReferenceError(
  71. "this hasn't been initialised - super() hasn't been called"
  72. );
  73. }
  74. return self;
  75. }
  76. function _isNativeReflectConstruct() {
  77. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  78. if (Reflect.construct.sham) return false;
  79. if (typeof Proxy === "function") return true;
  80. try {
  81. Boolean.prototype.valueOf.call(
  82. Reflect.construct(Boolean, [], function () {})
  83. );
  84. return true;
  85. } catch (e) {
  86. return false;
  87. }
  88. }
  89. function _getPrototypeOf(o) {
  90. _getPrototypeOf = Object.setPrototypeOf
  91. ? Object.getPrototypeOf
  92. : function _getPrototypeOf(o) {
  93. return o.__proto__ || Object.getPrototypeOf(o);
  94. };
  95. return _getPrototypeOf(o);
  96. }