_object-create.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // 19.1.2.2 / 15.2.3.5 Object.create(O [, Properties])
  2. var anObject = require('./_an-object')
  3. , dPs = require('./_object-dps')
  4. , enumBugKeys = require('./_enum-bug-keys')
  5. , IE_PROTO = require('./_shared-key')('IE_PROTO')
  6. , Empty = function(){ /* empty */ }
  7. , 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. , i = enumBugKeys.length
  13. , lt = '<'
  14. , gt = '>'
  15. , 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. };