object-create.js 3.0 KB

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