symbol.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const _symbols = [];
  4. const __internalMark__ = {};
  5. class HiddenSymbol {
  6. constructor(target) {
  7. Object.defineProperties(this, {
  8. target: {
  9. enumerable: false,
  10. writable: false,
  11. configurable: false,
  12. value: target,
  13. },
  14. });
  15. }
  16. }
  17. class InternalSymbol extends HiddenSymbol {
  18. constructor(target, __mark__) {
  19. if (__mark__ !== __internalMark__) {
  20. throw new TypeError('InternalSymbol cannot be constructed with new operator');
  21. }
  22. super(target);
  23. }
  24. static for(target) {
  25. for (let i = 0, len = _symbols.length; i < len; i++) {
  26. if (_symbols[i].target === target) {
  27. return _symbols[i].instance;
  28. }
  29. }
  30. const symbol = new InternalSymbol(target, __internalMark__);
  31. _symbols.push({
  32. target,
  33. instance: symbol,
  34. });
  35. return symbol;
  36. }
  37. }
  38. exports.InternalSymbol = InternalSymbol;
  39. exports.default = InternalSymbol;