class.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * DevExtreme (core/class.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var errors = require("./errors");
  11. var typeUtils = require("./utils/type");
  12. var wrapOverridden = function(baseProto, methodName, method) {
  13. return function() {
  14. var prevCallBase = this.callBase;
  15. this.callBase = baseProto[methodName];
  16. try {
  17. return method.apply(this, arguments)
  18. } finally {
  19. this.callBase = prevCallBase
  20. }
  21. }
  22. };
  23. var clonePrototype = function(obj) {
  24. var func = function() {};
  25. func.prototype = obj.prototype;
  26. return new func
  27. };
  28. var redefine = function(members) {
  29. var that = this;
  30. var overridden;
  31. var memberName;
  32. var member;
  33. if (!members) {
  34. return that
  35. }
  36. for (memberName in members) {
  37. member = members[memberName];
  38. overridden = "function" === typeof that.prototype[memberName] && "function" === typeof member;
  39. that.prototype[memberName] = overridden ? wrapOverridden(that.parent.prototype, memberName, member) : member
  40. }
  41. return that
  42. };
  43. var include = function() {
  44. var classObj = this;
  45. var argument;
  46. var name;
  47. var i;
  48. var hasClassObjOwnProperty = Object.prototype.hasOwnProperty.bind(classObj);
  49. var isES6Class = !hasClassObjOwnProperty("_includedCtors") && !hasClassObjOwnProperty("_includedPostCtors");
  50. if (isES6Class) {
  51. classObj._includedCtors = classObj._includedCtors.slice(0);
  52. classObj._includedPostCtors = classObj._includedPostCtors.slice(0)
  53. }
  54. for (i = 0; i < arguments.length; i++) {
  55. argument = arguments[i];
  56. if (argument.ctor) {
  57. classObj._includedCtors.push(argument.ctor)
  58. }
  59. if (argument.postCtor) {
  60. classObj._includedPostCtors.push(argument.postCtor)
  61. }
  62. for (name in argument) {
  63. if ("ctor" === name || "postCtor" === name) {
  64. continue
  65. }
  66. classObj.prototype[name] = argument[name]
  67. }
  68. }
  69. return classObj
  70. };
  71. var subclassOf = function(parentClass) {
  72. if (this.parent === parentClass) {
  73. return true
  74. }
  75. if (!this.parent || !this.parent.subclassOf) {
  76. return false
  77. }
  78. return this.parent.subclassOf(parentClass)
  79. };
  80. var abstract = function() {
  81. throw errors.Error("E0001")
  82. };
  83. var copyStatic = function() {
  84. var hasOwn = Object.prototype.hasOwnProperty;
  85. return function(source, destination) {
  86. for (var key in source) {
  87. if (!hasOwn.call(source, key)) {
  88. return
  89. }
  90. destination[key] = source[key]
  91. }
  92. }
  93. }();
  94. var classImpl = function() {};
  95. classImpl.inherit = function(members) {
  96. var inheritor = function() {
  97. if (!this || typeUtils.isWindow(this) || "function" !== typeof this.constructor) {
  98. throw errors.Error("E0003")
  99. }
  100. var instance = this;
  101. var ctor = instance.ctor;
  102. var includedCtors = instance.constructor._includedCtors;
  103. var includedPostCtors = instance.constructor._includedPostCtors;
  104. var i;
  105. for (i = 0; i < includedCtors.length; i++) {
  106. includedCtors[i].call(instance)
  107. }
  108. if (ctor) {
  109. ctor.apply(instance, arguments)
  110. }
  111. for (i = 0; i < includedPostCtors.length; i++) {
  112. includedPostCtors[i].call(instance)
  113. }
  114. };
  115. inheritor.prototype = clonePrototype(this);
  116. copyStatic(this, inheritor);
  117. inheritor.inherit = this.inherit;
  118. inheritor.abstract = abstract;
  119. inheritor.redefine = redefine;
  120. inheritor.include = include;
  121. inheritor.subclassOf = subclassOf;
  122. inheritor.parent = this;
  123. inheritor._includedCtors = this._includedCtors ? this._includedCtors.slice(0) : [];
  124. inheritor._includedPostCtors = this._includedPostCtors ? this._includedPostCtors.slice(0) : [];
  125. inheritor.prototype.constructor = inheritor;
  126. inheritor.redefine(members);
  127. return inheritor
  128. };
  129. classImpl.abstract = abstract;
  130. module.exports = classImpl;