proto-merge

1.0.1 • Public • Published

proto-merge

Build Status

Define complex prototype chains with vanilla nested objects.

Installation

node + browserify

$ npm install proto-merge

component

$ component install timoxley/proto-merge

Why?

APIs often need a little prototype magic, yet the overhead of verbose prototype definitions will ruin the elegance / brevity of the code.

proto-merge simplifies complex prototype chaining definitions to a minimum.

Example

var merge = require('proto-merge')
 
// all objects inherit from their 'parent' objects
var project = merge({
  // top level object becomes the 'parent' for any objects defined inside it
  name: 'Main Project',
  version: '0.0.1',
  sub_project: {
    // this will inherit from the parent
    name: 'Sub Project'
  },
  forks: [
    // each will inherit from the parent
    {version: '0.0.2'},
    {name: 'A fork!'}
  ]
})
 
// property access works as expected:
console.log(project.version) // => '0.0.1'
 
 
// sub_project inherits properties from from its 'parent'
console.log(project.sub_project.version) // => '0.0.1'
 
// sub_project can override with its own properties
console.log(project.sub_project.name) // => 'Sub Project'
 
// the link is dynamic, so if you change the parent
// you also change the child
project.version = '0.0.2'
 
console.log(project.sub_project.version) // => '0.0.2'
 
// prototypes are also inherited in array members
console.log(project.forks[0].version) // => '0.0.2'
console.log(project.forks[1].version) // => '0.0.1'
 

As with all magic, use responsibly.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i proto-merge

Weekly Downloads

2

Version

1.0.1

License

MIT

Last publish

Collaborators

  • timoxley