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