Skip to content

Instantly share code, notes, and snippets.

@benatkin
Created July 27, 2011 23:50
Show Gist options
  • Save benatkin/1110620 to your computer and use it in GitHub Desktop.
Save benatkin/1110620 to your computer and use it in GitHub Desktop.

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.

yepnope([
{
test: window.JSON,
nope: '//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js'
}, {
test: Object.getOwnPropertyNames,
nope: "//cdnjs.cloudflare.com/ajax/libs/es5-shim/1.2.4/es5-shim.min.js",
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(slice.call(arguments)));return a}else{return b.call.apply(b,c.concat(slice.call(arguments)))}}var b=this;if(typeof b.apply!=="function"||typeof b.call!=="function")return new TypeError;var c=slice.call(arguments);d.length=typeof b==="function"?Math.max(b.length-c.length,0):0;return d}}
}
}
]);
@benatkin
Copy link
Author

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