A tiny library that provides a function to create a prototype chain from multiple objects.
Stop writing this:
var Child = Object.create(Parent, {
foo: {
"value": 10
},
bar: {
"value": function() { /* ... */ }
}
});
And start writing this:
var Child = extend(Parent, {
foo: 10,
bar: function() { /* ... */ }
});
var extend = require('proto-extend');
var a = { id: 'a', foo: 1 };
var b = { id: 'b', bar: 2 };
var c = { id: 'c', baz: 3 };
var chain = extend(a, b, c);
The above returns a new object with a prototype chain of the following:
c -> b -> a -> Object.prototype
and has the properties:
{
id: 'c',
foo: 1,
bar: 2,
baz: 3
}
For more information on how the prototype chain works, see the MDN article Inheritance and the prototype chain.
The main export. Takes a base and any number of extension objects and returns a
new object. If the base is not an object, null
will be used instead. Each
extension is converted to a property descriptor map and a new object is created.
Extensions that are not an object are ignored.
A helper function used internally but exposed for convenience. It uses the
native methods on Object for getting a list of property names and property
descriptors and creates a map. The main purpose is to feed the second argument
of Object.create()
.
A helper function that takes a prototype chain and flattens it into a new single
object. This is useful if you are passing a prototypal-extended object to a
library that intentionally iterates over only "own" keys. If you specify a
base
, it will stop flattening when it reaches a prototype of that object. By
default, this is Object.prototype
, though it will always stop if it reaches
the null
prototype.