mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Ensure bound result of _.bind(func, …) is an instance of bound and func.
Former-commit-id: d8176ad5eb45a3d675617676fc1eee4d9cbd6ebc
This commit is contained in:
16
lodash.js
16
lodash.js
@@ -609,19 +609,19 @@
|
|||||||
: partialArgs;
|
: partialArgs;
|
||||||
}
|
}
|
||||||
if (this instanceof bound) {
|
if (this instanceof bound) {
|
||||||
// get `func` instance if `bound` is invoked in a `new` expression
|
|
||||||
noop.prototype = func.prototype;
|
|
||||||
thisBinding = new noop;
|
|
||||||
|
|
||||||
// mimic the constructor's `return` behavior
|
// mimic the constructor's `return` behavior
|
||||||
// http://es5.github.com/#x13.2.2
|
// http://es5.github.com/#x13.2.2
|
||||||
var result = func.apply(thisBinding, args);
|
var result = func.apply(this, args);
|
||||||
return isObject(result)
|
return isObject(result) ? result : this;
|
||||||
? result
|
|
||||||
: thisBinding
|
|
||||||
}
|
}
|
||||||
return func.apply(thisBinding, args);
|
return func.apply(thisBinding, args);
|
||||||
}
|
}
|
||||||
|
if (func && func.prototype) {
|
||||||
|
// ensure `new bound` is an instance of `bound` and `func`
|
||||||
|
noop.prototype = func.prototype;
|
||||||
|
bound.prototype = new noop
|
||||||
|
noop.prototype = null;
|
||||||
|
}
|
||||||
return bound;
|
return bound;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -209,6 +209,14 @@
|
|||||||
bound(['b'], 'c');
|
bound(['b'], 'c');
|
||||||
deepEqual(args, ['a', ['b'], 'c']);
|
deepEqual(args, ['a', ['b'], 'c']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('ensure `new bound` is an instance of `bound` and `func`', function() {
|
||||||
|
var func = function() {},
|
||||||
|
bound = _.bind(func, {}),
|
||||||
|
instance = new bound;
|
||||||
|
|
||||||
|
ok(instance instanceof bound && instance instanceof func);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user