Skip to content
Get the prototype chain of an object or primitive as an Array.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
History.md Release 1.0.5 Jun 30, 2016
LICENSE
Readme.md Update Readme.md Aug 9, 2016
index.js
package.json
protochain.js
test.js

Readme.md

protochain

Get a value's prototype chain as an Array.

Build Status

NPM NPM

Installation

> npm install protochain

Usage

const protochain = require('protochain')

Primitives

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]

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

Objects & null/undefined

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

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

protochain(null)
// => []

protochain(undefined)
// => []

protochain()
// => []

Errors

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

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

Classes

class Person {}
class FancyPerson extends Person {}

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

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

ES5 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]

Promises

protochain(Promise.resolve())
// => [Promise.prototype, Object.prototype]

Collections

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

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

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

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

Typed Arrays

Note: different hierarchy in newer JS engines.

protochain(new Int8Array())

// Newer Engines
// => [Int8Array.prototype, TypedArray.prototype, Object.prototype]

// Older Engines
// => [Int8Array.prototype, Object.prototype]

License

MIT

You can’t perform that action at this time.