diff --git a/lib/helpers/bind.js b/lib/helpers/bind.js new file mode 100644 index 0000000..6147c60 --- /dev/null +++ b/lib/helpers/bind.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = function bind(fn, thisArg) { + return function wrap() { + var args = new Array(arguments.length); + for (var i = 0; i < args.length; i++) { + args[i] = arguments[i]; + } + return fn.apply(thisArg, args); + }; +}; diff --git a/test/specs/helpers/bind.spec.js b/test/specs/helpers/bind.spec.js new file mode 100644 index 0000000..a02c462 --- /dev/null +++ b/test/specs/helpers/bind.spec.js @@ -0,0 +1,12 @@ +var bind = require('../../../lib/helpers/bind'); + +describe('bind', function () { + it('should bind an object to a function', function () { + var o = { val: 123 }; + var f = bind(function (num) { + return this.val * num; + }, o); + + expect(f(2)).toEqual(246); + }); +});