Last active
October 16, 2018 16:26
-
-
Save WebReflection/4397807 to your computer and use it in GitHub Desktop.
a meaningful shim limited to the first argument only of `Object.create()`, able to create empty dictionaries too such `inherit(null)` even in older browsers, down to IE5, without `__proto__` and/or without ES5 `Object.create()` support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*!(C) WebReflection *//** @license Mit Style */ | |
// inspired by https://gist.github.com/4395291 | |
this.inherit || (this.inherit = function(create){ | |
if (!create) { | |
if ({__proto__:null} instanceof Object) { | |
for (var | |
Null = function Null() {}, | |
doc = document, | |
html = doc.documentElement, | |
iframe = html.insertBefore( | |
doc.createElement("iframe"), | |
html.lastChild | |
), | |
// works down to IE7, it does NOT work in IE6 | |
NullPrototype = Null.prototype = | |
(iframe.src = "javascript:", iframe.contentWindow.Object.prototype), | |
/* this would work in IE6 too | |
idoc = iframe.contentWindow.document || | |
iframe.contentDocument || | |
iframe.document, | |
NullPrototype = Null.prototype = ( | |
idoc.open(), | |
idoc.write( | |
"<script>parent.inherit=Object.prototype<" + | |
"/script>" | |
), | |
idoc.close(), | |
inherit | |
), | |
//*/ | |
xtend = html.removeChild(iframe) && function xtend(object) { | |
return xtend.prototype === object ? | |
(xtend.prototype = xtend, this) : | |
new xtend(xtend.prototype = object) | |
; | |
}, | |
proto = [ | |
"hasOwnProperty", | |
"isPrototypeOf", | |
"propertyIsEnumerable", | |
"valueOf", | |
"toString", | |
"toLocaleString", | |
"constructor" | |
], | |
i = proto.length; i--; | |
) delete NullPrototype[proto[i]]; | |
// you know ... IE, leaks, and shit ... | |
create = doc = html = iframe = NullPrototype = proto = | |
function(object) { | |
return object == null ? new Null : xtend(object); | |
} | |
; | |
} else { | |
create = function(object) { | |
return {__proto__: object}; | |
}; | |
} | |
} | |
return function(object) { | |
return create(object); | |
}; | |
}(Object.create)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a link to the shim-sham issue w/ iframes and
document.domain
:github.com/kriskowal/es5-shim/issues/150.