From 4869b4c6cab74a52024d013cf8203a831a31386c Mon Sep 17 00:00:00 2001 From: Kit Goncharov Date: Sun, 20 Mar 2011 09:36:00 -0600 Subject: [PATCH] Issue #150: `_.bind` should allow binding functions to falsy values. --- test/functions.js | 7 ++++++- underscore.js | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/test/functions.js b/test/functions.js index f74bc7294..29f6ec7f7 100644 --- a/test/functions.js +++ b/test/functions.js @@ -20,10 +20,15 @@ $(document).ready(function() { var func = _.bind(func, this, 'curly'); equals(func(), 'hello: curly', 'the function was completely applied in advance'); - + var func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname }; func = _.bind(func, this, 'hello', 'moe', 'curly'); equals(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments'); + + func = function(context, message) { equals(this, context, message); }; + _.bind(func, 0, 0, 'can bind a function to `0`')(); + _.bind(func, '', '', 'can bind a function to an empty string')(); + _.bind(func, false, false, 'can bind a function to `false`')(); }); test("functions: bindAll", function() { diff --git a/underscore.js b/underscore.js index 3570486c4..2b7968ddf 100644 --- a/underscore.js +++ b/underscore.js @@ -412,7 +412,7 @@ if (nativeBind && func.bind === nativeBind) return func.bind.apply(func, slice.call(arguments, 1)); var args = slice.call(arguments, 2); return function() { - return func.apply(obj || {}, args.concat(slice.call(arguments))); + return func.apply(obj, args.concat(slice.call(arguments))); }; };