UNPKG

1.9 kBJavaScriptView Raw
1'use strict';
2var DESCRIPTORS = require('../internals/descriptors');
3var IE8_DOM_DEFINE = require('../internals/ie8-dom-define');
4var V8_PROTOTYPE_DEFINE_BUG = require('../internals/v8-prototype-define-bug');
5var anObject = require('../internals/an-object');
6var toPropertyKey = require('../internals/to-property-key');
7
8var $TypeError = TypeError;
9// eslint-disable-next-line es/no-object-defineproperty -- safe
10var $defineProperty = Object.defineProperty;
11// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
12var $getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
13var ENUMERABLE = 'enumerable';
14var CONFIGURABLE = 'configurable';
15var WRITABLE = 'writable';
16
17// `Object.defineProperty` method
18// https://tc39.es/ecma262/#sec-object.defineproperty
19exports.f = DESCRIPTORS ? V8_PROTOTYPE_DEFINE_BUG ? function defineProperty(O, P, Attributes) {
20 anObject(O);
21 P = toPropertyKey(P);
22 anObject(Attributes);
23 if (typeof O === 'function' && P === 'prototype' && 'value' in Attributes && WRITABLE in Attributes && !Attributes[WRITABLE]) {
24 var current = $getOwnPropertyDescriptor(O, P);
25 if (current && current[WRITABLE]) {
26 O[P] = Attributes.value;
27 Attributes = {
28 configurable: CONFIGURABLE in Attributes ? Attributes[CONFIGURABLE] : current[CONFIGURABLE],
29 enumerable: ENUMERABLE in Attributes ? Attributes[ENUMERABLE] : current[ENUMERABLE],
30 writable: false
31 };
32 }
33 } return $defineProperty(O, P, Attributes);
34} : $defineProperty : function defineProperty(O, P, Attributes) {
35 anObject(O);
36 P = toPropertyKey(P);
37 anObject(Attributes);
38 if (IE8_DOM_DEFINE) try {
39 return $defineProperty(O, P, Attributes);
40 } catch (error) { /* empty */ }
41 if ('get' in Attributes || 'set' in Attributes) throw new $TypeError('Accessors not supported');
42 if ('value' in Attributes) O[P] = Attributes.value;
43 return O;
44};