Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
what's the best way to check if one object is a prototype of another?

I believe the best way would be something like:

var a = {a:1, b:2}
var b = Object.create(a);
b.a = 1
var c = Object.create(b);
c.c = 3;
var d = Object.create(c);

d.protoTree();  //returns: [c, b, a]
c.protoTree();  //returns: [b, a];
b.protoTree();  //returns: [a];

But is this possible? What's the best way assuming you only know the D object?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use isPrototypeOf() for this use case

about 4 years ago · Juan Pablo Isaza Denunciar

0

If wouldn't be good to have that as a member method, but it's easy to implement as a standalone (recursive) function:

function protoTree(obj){
    if(obj === null) return []
    return [obj, ...protoTree(Object.getPrototypeOf(obj))]
}

It works like this (note that it also returns Object.prototype, that a inherits from.

var a = {a:1, b:2}
var b = Object.create(a);
b.a = 1
var c = Object.create(b);
c.c = 3;
var d = Object.create(c);

//Note that `protoTree` returns the prototype objects themselves, not the variable names they are stored in
protoTree(d);  //returns: [c, b, a, Object.prototype]
protoTree(c);  //returns: [b, a, Object.prototype]
protoTree(b);  //returns: [a, Object.prototype]
protoTree(a);  //returns: [Object.prototype]
about 4 years ago · Juan Pablo Isaza Denunciar

0

Using Object.prototype.isPrototypeOf() is the way to do this

function Person() {}
function Child() {}

Child.prototype = Object.create(Person.prototype);

const child = new Child();

console.log(Person.prototype.isPrototypeOf(child));

console.log(Child.prototype.isPrototypeOf(child));

More about Object.prototype.isPrototypeOf() here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda