better bind, use native fn.bind if available

This commit is contained in:
Vladimir Agafonkin 2013-11-27 18:27:29 +02:00
parent 294a7250d0
commit 88f21d01af
2 changed files with 14 additions and 6 deletions

View File

@ -49,13 +49,14 @@ describe('Util', function () {
var fn = sinon.spy(),
foo = {},
a = {},
b = {};
b = {},
c = {};
var fn2 = L.Util.bind(fn, foo, a, b);
fn2();
fn2(c);
expect(fn.calledWith(a, b)).to.be.ok();
expect(fn.calledWith(a, b, c)).to.be.ok();
});
});

View File

@ -18,10 +18,17 @@ L.Util = {
return dest;
},
bind: function (fn, obj) { // (Function, Object) -> Function
var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
bind: function (fn, obj) {
var slice = Array.prototype.slice;
if (fn.bind) {
return fn.bind.apply(fn, slice.call(arguments, 1));
}
var args = slice.call(arguments, 2);
return function () {
return fn.apply(obj, args || arguments);
return fn.apply(obj, args.length ? args.concat(slice.call(arguments)) : arguments);
};
},