create.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var baseAssign = require('../internal/baseAssign'),
  2. baseCreate = require('../internal/baseCreate'),
  3. isIterateeCall = require('../internal/isIterateeCall');
  4. /**
  5. * Creates an object that inherits from the given `prototype` object. If a
  6. * `properties` object is provided its own enumerable properties are assigned
  7. * to the created object.
  8. *
  9. * @static
  10. * @memberOf _
  11. * @category Object
  12. * @param {Object} prototype The object to inherit from.
  13. * @param {Object} [properties] The properties to assign to the object.
  14. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  15. * @returns {Object} Returns the new object.
  16. * @example
  17. *
  18. * function Shape() {
  19. * this.x = 0;
  20. * this.y = 0;
  21. * }
  22. *
  23. * function Circle() {
  24. * Shape.call(this);
  25. * }
  26. *
  27. * Circle.prototype = _.create(Shape.prototype, {
  28. * 'constructor': Circle
  29. * });
  30. *
  31. * var circle = new Circle;
  32. * circle instanceof Circle;
  33. * // => true
  34. *
  35. * circle instanceof Shape;
  36. * // => true
  37. */
  38. function create(prototype, properties, guard) {
  39. var result = baseCreate(prototype);
  40. if (guard && isIterateeCall(prototype, properties, guard)) {
  41. properties = undefined;
  42. }
  43. return properties ? baseAssign(result, properties) : result;
  44. }
  45. module.exports = create;