ObjectCreate.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $ObjectCreate = GetIntrinsic('%Object.create%', true);
  4. var $TypeError = require('es-errors/type');
  5. var $SyntaxError = require('es-errors/syntax');
  6. var IsArray = require('./IsArray');
  7. var Type = require('./Type');
  8. var forEach = require('../helpers/forEach');
  9. var SLOT = require('internal-slot');
  10. var hasProto = require('has-proto')();
  11. // https://262.ecma-international.org/6.0/#sec-objectcreate
  12. module.exports = function ObjectCreate(proto, internalSlotsList) {
  13. if (proto !== null && Type(proto) !== 'Object') {
  14. throw new $TypeError('Assertion failed: `proto` must be null or an object');
  15. }
  16. var slots = arguments.length < 2 ? [] : internalSlotsList; // step 1
  17. if (arguments.length >= 2 && !IsArray(slots)) {
  18. throw new $TypeError('Assertion failed: `internalSlotsList` must be an Array');
  19. }
  20. var O;
  21. if ($ObjectCreate) {
  22. O = $ObjectCreate(proto);
  23. } else if (hasProto) {
  24. O = { __proto__: proto };
  25. } else {
  26. if (proto === null) {
  27. throw new $SyntaxError('native Object.create support is required to create null objects');
  28. }
  29. var T = function T() {};
  30. T.prototype = proto;
  31. O = new T();
  32. }
  33. if (slots.length > 0) {
  34. forEach(slots, function (slot) {
  35. SLOT.set(O, slot, void undefined);
  36. });
  37. }
  38. return O; // step 6
  39. };