debian-mirror-gitlab/core-js/internals/object-create.js

79 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-09-25 13:30:05 +05:30
var anObject = require('../internals/an-object');
var defineProperties = require('../internals/object-define-properties');
var enumBugKeys = require('../internals/enum-bug-keys');
var hiddenKeys = require('../internals/hidden-keys');
var html = require('../internals/html');
var documentCreateElement = require('../internals/document-create-element');
var sharedKey = require('../internals/shared-key');
2019-12-21 20:55:43 +05:30
var GT = '>';
var LT = '<';
2019-09-25 13:30:05 +05:30
var PROTOTYPE = 'prototype';
2019-12-21 20:55:43 +05:30
var SCRIPT = 'script';
var IE_PROTO = sharedKey('IE_PROTO');
var EmptyConstructor = function () { /* empty */ };
var scriptTag = function (content) {
return LT + SCRIPT + GT + content + LT + '/' + SCRIPT + GT;
};
// Create object with fake `null` prototype: use ActiveX Object with cleared prototype
var NullProtoObjectViaActiveX = function (activeXDocument) {
activeXDocument.write(scriptTag(''));
activeXDocument.close();
var temp = activeXDocument.parentWindow.Object;
activeXDocument = null; // avoid memory leak
return temp;
};
2019-09-25 13:30:05 +05:30
// Create object with fake `null` prototype: use iframe Object with cleared prototype
2019-12-21 20:55:43 +05:30
var NullProtoObjectViaIFrame = function () {
2019-09-25 13:30:05 +05:30
// Thrash, waste and sodomy: IE GC bug
var iframe = documentCreateElement('iframe');
2019-12-21 20:55:43 +05:30
var JS = 'java' + SCRIPT + ':';
2019-09-25 13:30:05 +05:30
var iframeDocument;
iframe.style.display = 'none';
html.appendChild(iframe);
2019-12-21 20:55:43 +05:30
// https://github.com/zloirock/core-js/issues/475
iframe.src = String(JS);
2019-09-25 13:30:05 +05:30
iframeDocument = iframe.contentWindow.document;
iframeDocument.open();
2019-12-21 20:55:43 +05:30
iframeDocument.write(scriptTag('document.F=Object'));
2019-09-25 13:30:05 +05:30
iframeDocument.close();
2019-12-21 20:55:43 +05:30
return iframeDocument.F;
2019-09-25 13:30:05 +05:30
};
2019-12-21 20:55:43 +05:30
// 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
// avoid IE GC bug
var activeXDocument;
var NullProtoObject = function () {
try {
/* global ActiveXObject */
activeXDocument = document.domain && new ActiveXObject('htmlfile');
} catch (error) { /* ignore */ }
NullProtoObject = activeXDocument ? NullProtoObjectViaActiveX(activeXDocument) : NullProtoObjectViaIFrame();
var length = enumBugKeys.length;
while (length--) delete NullProtoObject[PROTOTYPE][enumBugKeys[length]];
return NullProtoObject();
};
hiddenKeys[IE_PROTO] = true;
2019-09-25 13:30:05 +05:30
// `Object.create` method
// https://tc39.github.io/ecma262/#sec-object.create
module.exports = Object.create || function create(O, Properties) {
var result;
if (O !== null) {
2019-12-21 20:55:43 +05:30
EmptyConstructor[PROTOTYPE] = anObject(O);
result = new EmptyConstructor();
EmptyConstructor[PROTOTYPE] = null;
2019-09-25 13:30:05 +05:30
// add "__proto__" for Object.getPrototypeOf polyfill
result[IE_PROTO] = O;
2019-12-21 20:55:43 +05:30
} else result = NullProtoObject();
2019-09-25 13:30:05 +05:30
return Properties === undefined ? result : defineProperties(result, Properties);
};