Add _.create.

This commit is contained in:
John-David Dalton
2013-10-19 01:08:09 -07:00
parent cd9332cddf
commit de8e6d7174
7 changed files with 364 additions and 255 deletions

124
lodash.js
View File

@@ -1279,7 +1279,7 @@
// non `Object` object instances with different constructors are not equal
if (ctorA != ctorB &&
!(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
(!nativeCreate || ('constructor' in a && 'constructor' in b))
('constructor' in a && 'constructor' in b)
) {
return false;
}
@@ -1619,7 +1619,7 @@
}
if (this instanceof bound) {
// ensure `new bound` is an instance of `func`
thisBinding = createObject(func.prototype);
thisBinding = create(func.prototype);
// mimic the constructor's `return` behavior
// http://es5.github.io/#x13.2.2
@@ -1682,28 +1682,6 @@
);
}
/**
* Creates a new object with the specified `prototype`.
*
* @private
* @param {Object} prototype The prototype object.
* @returns {Object} Returns the new object.
*/
function createObject(prototype) {
return isObject(prototype) ? nativeCreate(prototype) : {};
}
// fallback for browsers without `Object.create`
if (!nativeCreate) {
createObject = function(prototype) {
if (isObject(prototype)) {
noop.prototype = prototype;
var result = new noop;
noop.prototype = null;
}
return result || {};
};
}
/**
* Used by `escape` to convert characters to HTML entities.
*
@@ -2093,6 +2071,50 @@
return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
}
/**
* Creates a new object with the specified `prototype`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} prototype The prototype object.
* @returns {Object} Returns the new object.
* @example
*
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* function Circle() {
* Shape.call(this);
* }
*
* Circle.prototype = _.create(Shape.prototype);
* Circle.prototype.constructor = Circle;
*
* var circle = new Circle;
* circle instanceof Circle
* // => true
*
* circle instanceof Shape
* // => true
*/
function create(prototype) {
return isObject(prototype) ? nativeCreate(prototype) : {};
}
// fallback for browsers without `Object.create`
if (!nativeCreate) {
create = function(prototype) {
if (isObject(prototype)) {
noop.prototype = prototype;
var result = new noop;
noop.prototype = null;
}
return result || {};
};
}
/**
* Assigns own enumerable properties of source object(s) to the destination
* object for all destination properties that resolve to `undefined`. Once a
@@ -2237,18 +2259,20 @@
* @returns {Object} Returns `object`.
* @example
*
* function Dog(name) {
* this.name = name;
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* Dog.prototype.bark = function() {
* console.log('Woof, woof!');
* Shape.prototype.move = function(x, y) {
* this.x += x;
* this.y += y;
* };
*
* _.forIn(new Dog('Dagny'), function(value, key) {
* _.forIn(new Shape, function(value, key) {
* console.log(key);
* });
* // => logs 'bark' and 'name' (property order is not guaranteed across environments)
* // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments)
*/
var forIn = createIterator(eachIteratorOptions, forOwnIteratorOptions, {
'useHas': false
@@ -2267,18 +2291,20 @@
* @returns {Object} Returns `object`.
* @example
*
* function Dog(name) {
* this.name = name;
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* Dog.prototype.bark = function() {
* console.log('Woof, woof!');
* Shape.prototype.move = function(x, y) {
* this.x += x;
* this.y += y;
* };
*
* _.forInRight(new Dog('Dagny'), function(value, key) {
* _.forInRight(new Shape, function(value, key) {
* console.log(key);
* });
* // => logs 'name' and 'bark' assuming `_.forIn ` logs 'bark' and 'name'
* // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move'
*/
function forInRight(object, callback, thisArg) {
var pairs = [];
@@ -2720,18 +2746,18 @@
* @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
* @example
*
* function Person(name, age) {
* this.name = name;
* this.age = age;
* function Shape() {
* this.x = 0;
* this.y = 0;
* }
*
* _.isPlainObject(new Person('fred', 40));
* _.isPlainObject(new Shape);
* // => false
*
* _.isPlainObject([1, 2, 3]);
* // => false
*
* _.isPlainObject({ 'name': 'fred', 'age': 40 });
* _.isPlainObject({ 'x': 0, 'y': 0 });
* // => true
*/
var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) {
@@ -3015,7 +3041,7 @@
* @static
* @memberOf _
* @category Objects
* @param {Array|Object} collection The collection to iterate over.
* @param {Array|Object} object The object to iterate over.
* @param {Function} [callback=identity] The function called per iteration.
* @param {*} [accumulator] The custom accumulator value.
* @param {*} [thisArg] The `this` binding of `callback`.
@@ -3037,8 +3063,6 @@
*/
function transform(object, callback, accumulator, thisArg) {
var isArr = isArray(object);
callback = baseCreateCallback(callback, thisArg, 4);
if (accumulator == null) {
if (isArr) {
accumulator = [];
@@ -3046,12 +3070,15 @@
var ctor = object && object.constructor,
proto = ctor && ctor.prototype;
accumulator = createObject(proto);
accumulator = create(proto);
}
}
(isArr ? baseEach : forOwn)(object, function(value, index, object) {
return callback(accumulator, value, index, object);
});
if (callback) {
callback = baseCreateCallback(callback, thisArg, 4);
(isArr ? baseEach : forOwn)(object, function(value, index, object) {
return callback(accumulator, value, index, object);
});
}
return accumulator;
}
@@ -6545,6 +6572,7 @@
lodash.compact = compact;
lodash.compose = compose;
lodash.countBy = countBy;
lodash.create = create;
lodash.createCallback = createCallback;
lodash.curry = curry;
lodash.debounce = debounce;