Package detail

protochain

timoxley546.1kISC1.0.5

Get the prototype chain of any value as an Array

object, inherit, inheritance, prototypical

readme

protochain

Build Status

Get the prototype chain of an object or primitive as an Array.

Why

I often write this function, figure I should extract it. There are probably other utilities out there that do this but I couldn't find them so they're either poorly named/described or the search algorithm is not being very helpful or I simply searched for the wrong things.

Installation

> npm install protochain

Usage

ES5

var protochain = require('protochain')

protochain({})
// => [Object.prototype]

protochain(Object.create(null))
// => []

protochain(new Error('message'))
// => [Error.prototype, Object.prototype]

protochain(new TypeError('message'))
// => [TypeError.prototype, Error.prototype, Object.prototype]

// Inheritance

function Person() {

}

function FancyPerson() {
  Person.call(this)
}

FancyPerson.prototype = Object.create(Person.prototype)

protochain(new Person())
// => [Person.prototype, Object.prototype]

protochain(new FancyPerson())
// => [FancyPerson.prototype, Person.prototype, Object.prototype]

// Primitives are OK

protochain(123)
// => [Number.prototype, Object.prototype]

protochain('abc')
// => [String.prototype, Object.prototype]

protochain(/abc/)
// => [RegExp.prototype, Object.prototype]

protochain(true)
// => [Boolean.prototype, Object.prototype]

protochain(false)
// => [Boolean.prototype, Object.prototype]

// Null & Undefined === Empty List

protochain(null)
// => []

protochain(undefined)
// => []

protochain()
// => []

ES6


import protochain from 'protochain'

class Person {}
class FancyPerson extends Person {}

protochain(new Person())
// => [Person.prototype, Object.prototype]

protochain(new FancyPerson())
// => [FancyPerson.prototype, Person.prototype, Object.prototype])

License

MIT

changelog

1.0.5 / 2016-06-30

  • Fix default export for CommonJS compatibility. (@ljharb)

1.0.4 / 2016-06-30

  • Lint code with standard.
  • Build with babel 6.
  • Remove broken class extends Function test. Does not work with babel 6.
  • Add more tests, including tests for collections & typed arrays.
  • Mild internal refactoring.
  • Update tape.

1.0.3 / 2015-06-29

  • Update babel & tape devDependencies.
  • Functions are also not primitives. (@ljharb)

1.0.2 / 2015-03-07

  • Add History.md.
  • Avoid running Symbol test on platforms where Symbol is undefined.
  • More strict test suite.
  • Support Symbols. (@hemanth)
  • Add Travis CI.

1.0.1 / 2015-03-06

  • Add LICENSE.
  • Remove npm init added dependencies.

1.0.0 / 2015-03-06

  • Birth