Created
October 5, 2016 20:04
-
-
Save fbedussi/1511cdc56d4b29760df765c5ffb52898 to your computer and use it in GitHub Desktop.
Get root prototype on an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRootOf(obj) { | |
var proto = Object.getPrototypeOf(obj); | |
while (!proto.hasOwnProperty('id')) { | |
proto = Object.getPrototypeOf(proto); | |
} | |
return proto; | |
} | |
var Obj = {id: 1} | |
var obj1 = Object.create(Obj); | |
console.log(getRootOf(obj1)); | |
var obj2 = Object.create(obj1); | |
console.log(Object.getPrototypeOf(obj2)); | |
console.log(getRootOf(obj2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRootOf(obj) { | |
var proto = Object.getPrototypeOf(obj); | |
var emptyObjProto = Object.getPrototypeOf({}); | |
while (Object.getPrototypeOf(proto) !== emptyObjProto) { | |
proto = Object.getPrototypeOf(proto); | |
} | |
return proto; | |
} | |
var Obj = {id: 1} | |
var obj1 = Object.create(Obj); | |
console.log(getRootOf(obj1)); | |
var obj2 = Object.create(obj1); | |
console.log(Object.getPrototypeOf(obj2)); | |
console.log(getRootOf(obj2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment