Fix remove circular dependency in _.create by adding baseCreate.

This commit is contained in:
John-David Dalton
2013-10-19 16:20:15 -07:00
parent ed401199bd
commit 4a2bbb52c1
8 changed files with 325 additions and 317 deletions

View File

@@ -372,6 +372,29 @@
/*--------------------------------------------------------------------------*/
/**
* The base implementation of `_.create` without support for assigning
* properties to the created object.
*
* @private
* @param {Object} prototype The object to inherit from.
* @returns {Object} Returns the new object.
*/
function baseCreate(prototype, properties) {
return isObject(prototype) ? nativeCreate(prototype) : {};
}
// fallback for browsers without `Object.create`
if (!nativeCreate) {
baseCreate = function(prototype) {
if (isObject(prototype)) {
noop.prototype = prototype;
var result = new noop;
noop.prototype = null;
}
return result || {};
};
}
/**
* The base implementation of `_.createCallback` without support for creating
* "_.pluck" or "_.where" style callbacks.
@@ -716,7 +739,7 @@
}
if (this instanceof bound) {
// ensure `new bound` is an instance of `func`
thisBinding = create(func.prototype);
thisBinding = baseCreate(func.prototype);
// mimic the constructor's `return` behavior
// http://es5.github.io/#x13.2.2
@@ -973,54 +996,6 @@
: value;
}
/**
* Creates an object that inherits from the given `prototype` object. If a
* `properties` object is provided its own enumerable properties are assigned
* to the created object.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} prototype The object to inherit from.
* @param {Object} [properties] The properties to assign to the 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, { 'constructor': Circle });
*
* var circle = new Circle;
* circle instanceof Circle
* // => true
*
* circle instanceof Shape
* // => true
*/
function create(prototype, properties) {
var result = isObject(prototype) ? nativeCreate(prototype) : {};
return properties ? assign(result, properties) : result;
}
// fallback for browsers without `Object.create`
if (!nativeCreate) {
create = function(prototype) {
if (isObject(prototype)) {
noop.prototype = prototype;
var result = new noop;
noop.prototype = null;
}
result || (result = {});
return properties ? assign(result, properties) : result;
};
}
/**
* Assigns own enumerable properties of source object(s) to the destination
* object for all destination properties that resolve to `undefined`. Once a