main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $ObjectCreate = GetIntrinsic('%Object.create%', true);
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 | var $SyntaxError = require('es-errors/syntax');
|
---|
8 |
|
---|
9 | var IsArray = require('./IsArray');
|
---|
10 | var Type = require('./Type');
|
---|
11 |
|
---|
12 | var forEach = require('../helpers/forEach');
|
---|
13 |
|
---|
14 | var SLOT = require('internal-slot');
|
---|
15 |
|
---|
16 | var hasProto = require('has-proto')();
|
---|
17 |
|
---|
18 | // https://262.ecma-international.org/6.0/#sec-objectcreate
|
---|
19 |
|
---|
20 | module.exports = function ObjectCreate(proto, internalSlotsList) {
|
---|
21 | if (proto !== null && Type(proto) !== 'Object') {
|
---|
22 | throw new $TypeError('Assertion failed: `proto` must be null or an object');
|
---|
23 | }
|
---|
24 | var slots = arguments.length < 2 ? [] : internalSlotsList; // step 1
|
---|
25 | if (arguments.length >= 2 && !IsArray(slots)) {
|
---|
26 | throw new $TypeError('Assertion failed: `internalSlotsList` must be an Array');
|
---|
27 | }
|
---|
28 |
|
---|
29 | var O;
|
---|
30 | if ($ObjectCreate) {
|
---|
31 | O = $ObjectCreate(proto);
|
---|
32 | } else if (hasProto) {
|
---|
33 | O = { __proto__: proto };
|
---|
34 | } else {
|
---|
35 | if (proto === null) {
|
---|
36 | throw new $SyntaxError('native Object.create support is required to create null objects');
|
---|
37 | }
|
---|
38 | var T = function T() {};
|
---|
39 | T.prototype = proto;
|
---|
40 | O = new T();
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (slots.length > 0) {
|
---|
44 | forEach(slots, function (slot) {
|
---|
45 | SLOT.set(O, slot, void undefined);
|
---|
46 | });
|
---|
47 | }
|
---|
48 |
|
---|
49 | return O; // step 6
|
---|
50 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.