Skip to content

Commit

Permalink
alternative for object.create under IE8 #727
Browse files Browse the repository at this point in the history
  • Loading branch information
pwxu committed Dec 14, 2019
1 parent 6dcf223 commit 6214075
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions packages/core-js/internals/object-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@ var IE_PROTO = sharedKey('IE_PROTO');
var PROTOTYPE = 'prototype';
var EmptyConstructor = function () { /* empty */ };

// Check for document.domain and active x support
// No need to use active x approach when document.domain is not set
// see https://github.com/es-shims/es5-shim/issues/150
// variation of https://github.com/kitcambridge/es5-shim/commit/4f738ac066346
var viaActiveX = function () {
try {
return document.domain && new ActiveXObject('htmlfile');
} catch (e) {
return false;
}
};

// Create object with fake `null` prototype: use ActiveX Object
var NullProtoObjectViaActiveX = function () {
var activeXDocument = new ActiveXObject('htmlfile');
activeXDocument.write('<script><\/script>');
activeXDocument.close();
var empty = activeXDocument.parentWindow.Object.prototype;
activeXDocument = null;
return empty;
};

// Create object with fake `null` prototype: use iframe Object with cleared prototype
var NullProtoObject = function () {
var NullProtoObjectViaIFrame = function () {
// Thrash, waste and sodomy: IE GC bug
var iframe = documentCreateElement('iframe');
var length = enumBugKeys.length;
Expand All @@ -28,9 +50,9 @@ var NullProtoObject = function () {
iframeDocument.open();
iframeDocument.write(LT + SCRIPT + GT + 'document.F=Object' + LT + '/' + SCRIPT + GT);
iframeDocument.close();
NullProtoObject = iframeDocument.F;
while (length--) delete NullProtoObject[PROTOTYPE][enumBugKeys[length]];
return NullProtoObject();
NullProtoObjectViaIFrame = iframeDocument.F;
while (length--) delete NullProtoObjectViaIFrame[PROTOTYPE][enumBugKeys[length]];
return NullProtoObjectViaIFrame();
};

hiddenKeys[IE_PROTO] = true;
Expand All @@ -45,6 +67,6 @@ module.exports = Object.create || function create(O, Properties) {
EmptyConstructor[PROTOTYPE] = null;
// add "__proto__" for Object.getPrototypeOf polyfill
result[IE_PROTO] = O;
} else result = NullProtoObject();
} else result = viaActiveX() ? NullProtoObjectViaActiveX() : NullProtoObjectViaIFrame();
return Properties === undefined ? result : defineProperties(result, Properties);
};

0 comments on commit 6214075

Please sign in to comment.