ObjectDefineProperties.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var callBound = require('call-bind/callBound');
  3. var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
  4. var FromPropertyDescriptor = require('./FromPropertyDescriptor');
  5. var Get = require('./Get');
  6. var ToObject = require('./ToObject');
  7. var ToPropertyDescriptor = require('./ToPropertyDescriptor');
  8. var forEach = require('../helpers/forEach');
  9. var getOwnPropertyDescriptor = require('../helpers/getOwnPropertyDescriptor');
  10. var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
  11. var $push = callBound('Array.prototype.push');
  12. // https://262.ecma-international.org/6.0/#sec-objectdefineproperties
  13. module.exports = function ObjectDefineProperties(O, Properties) {
  14. var props = ToObject(Properties); // step 1
  15. var keys = OwnPropertyKeys(props); // step 2
  16. var descriptors = []; // step 3
  17. forEach(keys, function (nextKey) { // step 4
  18. var propDesc = ToPropertyDescriptor(getOwnPropertyDescriptor(props, nextKey)); // step 4.a
  19. if (typeof propDesc !== 'undefined' && propDesc['[[Enumerable]]']) { // step 4.b
  20. var descObj = Get(props, nextKey); // step 4.b.i
  21. var desc = ToPropertyDescriptor(descObj); // step 4.b.ii
  22. $push(descriptors, [nextKey, desc]); // step 4.b.iii
  23. }
  24. });
  25. forEach(descriptors, function (pair) { // step 5
  26. var P = pair[0]; // step 5.a
  27. var desc = pair[1]; // step 5.b
  28. desc = FromPropertyDescriptor(desc); // TODO: remove this once DefinePropertyOrThrow is fixed
  29. DefinePropertyOrThrow(O, P, desc); // step 5.c
  30. });
  31. return O; // step 6
  32. };