Fix issues preventing _.mixin from working as a module.

Former-commit-id: 57afac8d0901856aaf67688d46e2649532edeb61
This commit is contained in:
John-David Dalton
2013-08-01 09:18:31 -07:00
parent 067558529c
commit 907ac05c00

View File

@@ -5202,19 +5202,6 @@
* *
* _.filter(stooges, 'age__gt45'); * _.filter(stooges, 'age__gt45');
* // => [{ 'name': 'larry', 'age': 50 }] * // => [{ 'name': 'larry', 'age': 50 }]
*
* // create mixins with support for "_.pluck" and "_.where" callback shorthands
* _.mixin({
* 'toLookup': function(collection, callback, thisArg) {
* callback = _.createCallback(callback, thisArg);
* return _.reduce(collection, function(result, value, index, collection) {
* return (result[callback(value, index, collection)] = value, result);
* }, {});
* }
* });
*
* _.toLookup(stooges, 'name');
* // => { 'moe': { 'name': 'moe', 'age': 40 }, 'larry': { 'name': 'larry', 'age': 50 } }
*/ */
function createCallback(func, thisArg, argCount) { function createCallback(func, thisArg, argCount) {
var type = typeof func; var type = typeof func;
@@ -5681,15 +5668,18 @@
* // => 'Moe' * // => 'Moe'
*/ */
function mixin(object, source) { function mixin(object, source) {
var ctor = object,
isFunc = !source || isFunction(ctor);
if (!source) { if (!source) {
ctor = lodashWrapper;
source = object; source = object;
object = lodash; object = lodash;
} }
var isFunc = isFunction(object);
forEach(functions(source), function(methodName) { forEach(functions(source), function(methodName) {
var func = object[methodName] = source[methodName]; var func = object[methodName] = source[methodName];
if (isFunc) { if (isFunc) {
object.prototype[methodName] = function() { ctor.prototype[methodName] = function() {
var value = this.__wrapped__, var value = this.__wrapped__,
args = [value]; args = [value];
@@ -5697,7 +5687,7 @@
var result = func.apply(object, args); var result = func.apply(object, args);
return (value && typeof value == 'object' && value === result) return (value && typeof value == 'object' && value === result)
? this ? this
: new lodashWrapper(result); : new ctor(result);
}; };
} }
}); });