npm.io
1.0.5 • Published 10 years ago

protochain

Licence
ISC
Version
1.0.5
Deps
0
Vulns
0
Weekly
0
Stars
9

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

Keywords