Created
May 23, 2016 22:23
-
-
Save remarkablemark/f5ff311297311565b6046b6ca96ae39f to your computer and use it in GitHub Desktop.
Recreate the `bind` method using functional JavaScript.
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
'use strict'; | |
/** | |
* Create a bound function. | |
* | |
* @param {Function} func - The function to be bound. | |
* @param {Array} args - The function arguments. | |
* @param {*} [context] - The function context. | |
*/ | |
function bind(func, args, context) { | |
return func.apply(context, args); | |
} | |
// test | |
function logX(a) { | |
console.log(this.x + a); | |
} | |
var obj1 = { x: 1 }; | |
var obj2 = { x: 2 }; | |
bind(logX, ['foo'], obj1); // 1foo | |
bind(logX, ['bar'], obj2); // 2bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment