Skip to content

Instantly share code, notes, and snippets.

Created July 27, 2011 23:50
What would you like to do?

shiminy: minimal es5-shim

Looking at the ES5 compatibility table, several browsers provide everything often used (i. e. not Object.freeze() and friends) except Function.prototype.bind. So I just inline that. Browsers that don't provide other functions also don't include Object.getOwnPropertyNames, so I used it as my test for including the entire shim.

Note that I haven't tested it yet, and it doesn't work for the six Object functions next to each other in the Safari 5 part of the compatibility chart (Safari 5 is deemed good enough).

Update: I added JSON for IE <= 7. JSON is used in a good number of node.js modules.

test: window.JSON,
nope: '//'
}, {
test: Object.getOwnPropertyNames,
nope: "//",
callback: function() {
// from es5-shim referenced above. Copyright the original authors. MIT License.
if(!Function.prototype.bind){var slice=Array.prototype.slice;Function.prototype.bind=function a(a){function d(){if(this instanceof d){var a=Object.create(b.prototype);b.apply(a,c.concat(;return a}else{return,c.concat(}}var b=this;if(typeof b.apply!=="function"||typeof!=="function")return new TypeError;var;d.length=typeof b==="function"?Math.max(b.length-c.length,0):0;return d}}
Copy link

I was thinking about this within the context of browserify. I realized that JSON.parse is a biggie too, and should be tested for separately, in order to hopefully support a lot of node modules that don't depend on modules that won't run in the browser.

I'm editing this to add json2.

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