Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Created May 23, 2016 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remarkablemark/f5ff311297311565b6046b6ca96ae39f to your computer and use it in GitHub Desktop.
Save remarkablemark/f5ff311297311565b6046b6ca96ae39f to your computer and use it in GitHub Desktop.
Recreate the `bind` method using functional JavaScript.
'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