public
Last active

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.

  • Download Gist
inherit.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*!(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));

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).

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.