_object-create.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // 19.1.2.2 / 15.2.3.5 Object.create(O [, Properties])
  2. var anObject = require('./_an-object');
  3. var dPs = require('./_object-dps');
  4. var enumBugKeys = require('./_enum-bug-keys');
  5. var IE_PROTO = require('./_shared-key')('IE_PROTO');
  6. var Empty = function () { /* empty */ };
  7. var PROTOTYPE = 'prototype';
  8. // Create object with fake `null` prototype: use iframe Object with cleared prototype
  9. var createDict = function () {
  10. // Thrash, waste and sodomy: IE GC bug
  11. var iframe = require('./_dom-create')('iframe');
  12. var i = enumBugKeys.length;
  13. var lt = '<';
  14. var gt = '>';
  15. var iframeDocument;
  16. iframe.style.display = 'none';
  17. require('./_html').appendChild(iframe);
  18. iframe.src = 'javascript:'; // eslint-disable-line no-script-url
  19. // createDict = iframe.contentWindow.Object;
  20. // html.removeChild(iframe);
  21. iframeDocument = iframe.contentWindow.document;
  22. iframeDocument.open();
  23. iframeDocument.write(lt + 'script' + gt + 'document.F=Object' + lt + '/script' + gt);
  24. iframeDocument.close();
  25. createDict = iframeDocument.F;
  26. while (i--) delete createDict[PROTOTYPE][enumBugKeys[i]];
  27. return createDict();
  28. };
  29. module.exports = Object.create || function create(O, Properties) {
  30. var result;
  31. if (O !== null) {
  32. Empty[PROTOTYPE] = anObject(O);
  33. result = new Empty();
  34. Empty[PROTOTYPE] = null;
  35. // add "__proto__" for Object.getPrototypeOf polyfill
  36. result[IE_PROTO] = O;
  37. } else result = createDict();
  38. return Properties === undefined ? result : dPs(result, Properties);
  39. };