Safely install functions on object prototypes.



Safely add methods to native object prototypes.

  • Doesn't overwrite existing properties with same name by default for safety.
  • If you do overwrite properties, you can access and restore them at any time.
  • Automatically forwards "this" as first argument to installed functions. This is the magic that makes non-prototype functions work as prototype functions.
$ npm install --save nativity
var nativity = require('nativity');
var _ = require('underscore');

nativity.install(Array.prototype, _, ['findWhere', 'pluck']);

var people = [ { name: 'Tina', age: 45 }, { name: 'Todd', age: 48 }, { name: 'Tucker', age: 10 } ];

console.log(people.findWhere({ name: 'Todd' })); // { name: 'Todd', age: 48 }

console.log(people.pluck('name')); // ['Tina', Todd', 'Tucker']

You can install a single method:

nativity.installOne(String.prototype, _.template, 'format');

console.log('Hello <%= name %>!'.format({ name: 'Bartholomew' })); // Hello Bartholomew!

Assigns the props from the source object to the destination object. Functions are converted to forward 'this' as the first argument.

nativity.install(destObject, srcObject[, props][, options]);

Assigns the value to the source object at the given key. If the value is a function it will be converted to forward 'this' as the first argument.

nativity.installOne(destObject, value, key[, options]);


nativity.install(Array.prototype, _, ['findWhere', 'pluck'], {
// do not override existing properties on the host object
safe: true,

// only add properties that are functions
functionsOnly: false,

// specifies the argument index at which to forward 'this'
thisIndex: 0


Nativity installs methods on objects safely, that is, you can access overwritten properties and restore them at any time.

var mylib = {
    map: function(arr) { return 'blurp' },
    filter: function(arr) { return 'skronk' },
    dud: function() { return 'dud'; }
var installation = nativity.install(Array.prototype, mylib, { safe: false });

console.log([1,2,3].map(function(n) { return n*n; })); // 'blurp'

console.log(installation.overwritten); // 2

console.log(; // { dud: [Function] }

console.log(installation.prev); // { map: [Function], filter: [Function] }

installation.restore(); console.log([1,2,3].map(function(n) { return n*n; })); // [1,4,9]

Technically, nativity is written such that it can add properties to any object (i.e. _.defaults). It is the automatic forwarding of "this" as the first argument to installed methods that makes it special.

_.pluck(people, 'name')

Why is this better?

  • Mimics Subject-Verb-Object structure of the English language
  • More object-oriented
  • Chainable

Yes we have those

$ mocha
$ gulp --require coffee-script/register

Nativity is designed for library authors to expose appropriate methods for use on native object prototypes in a safe manner. See the nativity plugins below for examples. Submit a pull request to add yours to the list.