get-prototype-chain
Advanced tools
Comparing version
module.exports = function getPrototypeChain(obj) { | ||
var chain = []; | ||
var chain = [obj]; | ||
var prototype = obj; | ||
while (prototype = Object.getPrototypeOf(prototype)) { | ||
chain.push(prototype); | ||
} | ||
} | ||
return chain; | ||
}; |
{ | ||
"name": "get-prototype-chain", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Returns an array of the object's prototype chain", | ||
@@ -5,0 +5,0 @@ "main": "get-prototype-chain.js", |
@@ -33,3 +33,3 @@ # get-prototype-chain [](https://travis-ci.org/leahciMic/get-prototype-chain) | ||
// returns ['C', 'B', 'A', 'Object'] | ||
// returns ['C', 'C', 'B', 'A', 'Object'] | ||
``` |
@@ -6,17 +6,31 @@ 'use strict'; | ||
describe('prototype-chain', () => { | ||
class A { | ||
describe('complex hierarchy', () => { | ||
class A { | ||
} | ||
class B extends A { | ||
} | ||
class B extends A { | ||
} | ||
class C extends B { | ||
} | ||
class C extends B { | ||
} | ||
const Obj = new C(); | ||
} | ||
it('should walk the prototype chain and return objects', () => { | ||
const chain = prototypeChain(Obj).map(x => x.constructor.name); | ||
expect(chain).toEqual(['C', 'B', 'A', 'Object']); | ||
const Obj = new C(); | ||
it('should walk the prototype chain and return objects', () => { | ||
const chain = prototypeChain(Obj).map(x => x.constructor.name); | ||
// we expect to see C twice, one for the instance, and one for it's | ||
// prototype | ||
expect(chain).toEqual(['C', 'C', 'B', 'A', 'Object']); | ||
}); | ||
}); | ||
describe('simple object', () => { | ||
const Obj = {}; | ||
it('should walk the prototype chain and return objects', () => { | ||
const chain = prototypeChain(Obj).map(x => x.constructor.name); | ||
expect(chain).toEqual(['Object', 'Object']); | ||
}); | ||
}); | ||
}); |
2481
21.56%43
34.38%