Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active October 16, 2018 16:26
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
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.
/*!(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));
@WebReflection
Copy link
Author

@jdalton
Copy link

jdalton commented Mar 7, 2013

Yap your IE solution is like the one in shim-sham. It can be extended to work in IE6 https using ActiveXObject('htmlfile') instead. It's less code too. I did this with sandboxed natives (one of the techniques was getting natives from an iframe, the other from an ActiveX control, and then lastly custom constructors that rewired object's proto's).

@jdalton
Copy link

jdalton commented Mar 7, 2013

Here is a link to the shim-sham issue w/ iframes and document.domain:
github.com/kriskowal/es5-shim/issues/150.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment