object-create.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* global ActiveXObject -- old IE, WSH */
  2. var anObject = require('../internals/an-object');
  3. var definePropertiesModule = require('../internals/object-define-properties');
  4. var enumBugKeys = require('../internals/enum-bug-keys');
  5. var hiddenKeys = require('../internals/hidden-keys');
  6. var html = require('../internals/html');
  7. var documentCreateElement = require('../internals/document-create-element');
  8. var sharedKey = require('../internals/shared-key');
  9. var GT = '>';
  10. var LT = '<';
  11. var PROTOTYPE = 'prototype';
  12. var SCRIPT = 'script';
  13. var IE_PROTO = sharedKey('IE_PROTO');
  14. var EmptyConstructor = function () { /* empty */ };
  15. var scriptTag = function (content) {
  16. return LT + SCRIPT + GT + content + LT + '/' + SCRIPT + GT;
  17. };
  18. // Create object with fake `null` prototype: use ActiveX Object with cleared prototype
  19. var NullProtoObjectViaActiveX = function (activeXDocument) {
  20. activeXDocument.write(scriptTag(''));
  21. activeXDocument.close();
  22. var temp = activeXDocument.parentWindow.Object;
  23. activeXDocument = null; // avoid memory leak
  24. return temp;
  25. };
  26. // Create object with fake `null` prototype: use iframe Object with cleared prototype
  27. var NullProtoObjectViaIFrame = function () {
  28. // Thrash, waste and sodomy: IE GC bug
  29. var iframe = documentCreateElement('iframe');
  30. var JS = 'java' + SCRIPT + ':';
  31. var iframeDocument;
  32. iframe.style.display = 'none';
  33. html.appendChild(iframe);
  34. // https://github.com/zloirock/core-js/issues/475
  35. iframe.src = String(JS);
  36. iframeDocument = iframe.contentWindow.document;
  37. iframeDocument.open();
  38. iframeDocument.write(scriptTag('document.F=Object'));
  39. iframeDocument.close();
  40. return iframeDocument.F;
  41. };
  42. // Check for document.domain and active x support
  43. // No need to use active x approach when document.domain is not set
  44. // see https://github.com/es-shims/es5-shim/issues/150
  45. // variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
  46. // avoid IE GC bug
  47. var activeXDocument;
  48. var NullProtoObject = function () {
  49. try {
  50. activeXDocument = new ActiveXObject('htmlfile');
  51. } catch (error) { /* ignore */ }
  52. NullProtoObject = typeof document != 'undefined'
  53. ? document.domain && activeXDocument
  54. ? NullProtoObjectViaActiveX(activeXDocument) // old IE
  55. : NullProtoObjectViaIFrame()
  56. : NullProtoObjectViaActiveX(activeXDocument); // WSH
  57. var length = enumBugKeys.length;
  58. while (length--) delete NullProtoObject[PROTOTYPE][enumBugKeys[length]];
  59. return NullProtoObject();
  60. };
  61. hiddenKeys[IE_PROTO] = true;
  62. // `Object.create` method
  63. // https://tc39.es/ecma262/#sec-object.create
  64. // eslint-disable-next-line es/no-object-create -- safe
  65. module.exports = Object.create || function create(O, Properties) {
  66. var result;
  67. if (O !== null) {
  68. EmptyConstructor[PROTOTYPE] = anObject(O);
  69. result = new EmptyConstructor();
  70. EmptyConstructor[PROTOTYPE] = null;
  71. // add "__proto__" for Object.getPrototypeOf polyfill
  72. result[IE_PROTO] = O;
  73. } else result = NullProtoObject();
  74. return Properties === undefined ? result : definePropertiesModule.f(result, Properties);
  75. };