source: node_modules/core-js-pure/internals/object-create.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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