odin-js-fundamentals-part-4/node_modules/es-abstract/2017/ObjectCreate.js

51 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2024-01-05 19:30:21 +00:00
'use strict';
var GetIntrinsic = require('get-intrinsic');
var $ObjectCreate = GetIntrinsic('%Object.create%', true);
var $TypeError = GetIntrinsic('%TypeError%');
var $SyntaxError = GetIntrinsic('%SyntaxError%');
var IsArray = require('./IsArray');
var Type = require('./Type');
var forEach = require('../helpers/forEach');
var SLOT = require('internal-slot');
var hasProto = require('has-proto')();
// https://262.ecma-international.org/6.0/#sec-objectcreate
module.exports = function ObjectCreate(proto, internalSlotsList) {
if (proto !== null && Type(proto) !== 'Object') {
throw new $TypeError('Assertion failed: `proto` must be null or an object');
}
var slots = arguments.length < 2 ? [] : internalSlotsList; // step 1
if (arguments.length >= 2 && !IsArray(slots)) {
throw new $TypeError('Assertion failed: `internalSlotsList` must be an Array');
}
var O;
if ($ObjectCreate) {
O = $ObjectCreate(proto);
} else if (hasProto) {
O = { __proto__: proto };
} else {
if (proto === null) {
throw new $SyntaxError('native Object.create support is required to create null objects');
}
var T = function T() {};
T.prototype = proto;
O = new T();
}
if (slots.length > 0) {
forEach(slots, function (slot) {
SLOT.set(O, slot, void undefined);
});
}
return O; // step 6
};