Created
July 29, 2014 15:53
-
-
Save ryanwilliamquinn/6e9194627a1dd6263a15 to your computer and use it in GitHub Desktop.
sorting out function.bind.bind
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
1. Function.bind.bind(Function.call) = Function.call.bind | |
2. Function.call.bind(someOtherFn) = someOtherFn.call | |
3. someOtherFn.call(newThisValue) | |
First you set up the 'binder' function. Note that 'binder' is not an official term, I just made it up: | |
var bb = Function.bind.bind(Function.call); // bb ends up as a function: Function.call.bind | |
then you can use it like so: | |
var boundfn = bb(someOtherFn); // boundfn is now a function: someOtherFn.call | |
which is a shortcurt to the equivalent statement: | |
var boundfn = someOtherFn.call.bind(someOtherFn); | |
Here is one example of each way of doing it: | |
var binder = Function.bind.bind(Function.call); | |
var myfncall2 = binder(myfn); | |
myfncall2({test: 'this other crap'}); | |
var myfn = function () {console.log("testing: " + this.test)}; | |
var myfncall = myfn.call.bind(myfn); | |
myfncall({test: 'this crap'}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment