forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.js
39 lines (38 loc) · 975 Bytes
/
create.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Creates an object that inherits from the `prototype` object. If a
* `properties` object is given, its own enumerable string keyed properties
* are assigned to the created object.
*
* @since 2.3.0
* @category Object
* @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
* })
*
* const circle = new Circle
* circle instanceof Circle
* // => true
*
* circle instanceof Shape
* // => true
*/
function create(prototype, properties) {
prototype = prototype === null ? null : Object(prototype)
const result = Object.create(prototype)
return properties == null ? result : Object.assign(result, properties)
}
export default create