1.0.5 • Published 8 years ago

protochain v1.0.5

Weekly downloads
71,616
License
ISC
Repository
github
Last release
8 years ago

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