Created
October 25, 2011 11:10
-
-
Save dsingleton/1312328 to your computer and use it in GitHub Desktop.
Polyfill for Function.prototype.bind
This file contains hidden or 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
Function.prototype.bind=Function.prototype.bind||function(b){if(typeof this!=="function"){throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");}var a=Array.prototype.slice,f=a.call(arguments,1),e=this,c=function(){},d=function(){return e.apply(this instanceof c?this:b||window,f.concat(a.call(arguments)));};c.prototype=this.prototype;d.prototype=new c();return d;}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For extra smallness, here are a few "clean" (JSHint friendly) optimizations :)
Array.prototype.slice
can be replaced with[]
. -saves 19 charactersFunction.prototype.bind
can be replaced with(function(){}).bind
. -saves 4 characters,c=function(){}
, located in a comma separated list of variable declarations can be replaced with a function declaration (function c(){}
), if it is placed before the variable declarations. -saves 1 characterSee my fork: https://gist.github.com/Daniel-Hug/5682738