Last active
August 29, 2015 13:56
-
-
Save wizard04wsu/8831744 to your computer and use it in GitHub Desktop.
Cross-browser workaround for function binding
This file contains 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
//creates a new function that, when called, has its `this` keyword set to the provided object, with a given sequence of arguments | |
// preceding any provided when the new function is called | |
//note: this is only a partial implementation; it may not work as expected in some scenarios. | |
// For instance, the resulting function cannot be used as a contructor. | |
if(!Function.prototype.bind){ | |
Function.prototype.bind = function bind(toObject){ | |
"use strict"; | |
var originalFn, boundArgs; | |
if(typeof this !== "function") throw new TypeError("Owner object must be a Function"); | |
if(typeof toObject === "undefined" || toObject === null) throw new TypeError("toObject must be an object"); | |
originalFn = this; | |
boundArgs = Array.prototype.slice.call(arguments, 1); | |
return function (){ | |
var additionalArgs = Array.prototype.slice.call(arguments, 0); | |
return originalFn.apply(toObject, boundArgs.concat(additionalArgs)); | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment