DefineMethodProperty.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
  4. var IsExtensible = require('./IsExtensible');
  5. var IsPropertyKey = require('./IsPropertyKey');
  6. var Type = require('./Type');
  7. // https://262.ecma-international.org/13.0/#sec-definemethodproperty
  8. module.exports = function DefineMethodProperty(homeObject, key, closure, enumerable) {
  9. if (Type(homeObject) !== 'Object') {
  10. throw new $TypeError('Assertion failed: `homeObject` is not an Object');
  11. }
  12. if (!IsPropertyKey(key)) {
  13. throw new $TypeError('Assertion failed: `key` is not a Property Key or a Private Name');
  14. }
  15. if (typeof closure !== 'function') {
  16. throw new $TypeError('Assertion failed: `closure` is not a function');
  17. }
  18. if (typeof enumerable !== 'boolean') {
  19. throw new $TypeError('Assertion failed: `enumerable` is not a Boolean');
  20. }
  21. // 1. Assert: homeObject is an ordinary, extensible object with no non-configurable properties.
  22. if (!IsExtensible(homeObject)) {
  23. throw new $TypeError('Assertion failed: `homeObject` is not an ordinary, extensible object, with no non-configurable properties');
  24. }
  25. // 2. If key is a Private Name, then
  26. // a. Return PrivateElement { [[Key]]: key, [[Kind]]: method, [[Value]]: closure }.
  27. // 3. Else,
  28. var desc = { // step 3.a
  29. '[[Value]]': closure,
  30. '[[Writable]]': true,
  31. '[[Enumerable]]': enumerable,
  32. '[[Configurable]]': true
  33. };
  34. DefinePropertyOrThrow(homeObject, key, desc); // step 3.b
  35. };