Skip to content

dpatti/proto-extend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

proto-extend

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() { /* ... */ }
});

Usage

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.

API

extend(base, extensions...) -> Object

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.

extend.getOwnPropertyDescriptorMap(object) -> Object

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().

extend.flatten(object[, base]) -> Object

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.

About

Extend objects in JavaScript using the prototype chain

Resources

License

Stars

Watchers

Forks

Packages