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

286
Vistas
How does the prototype lookup work in isPrototypeOf on inherited classes?

Taking the following code as a starting point:

class SuperKlass {};
class SubKlass extends SuperKlass {};

const superKlass = new SuperKlass();
const subKlass = new SubKlass();

console.log(SuperKlass.isPrototypeOf(SubKlass));
console.log(SuperKlass.prototype.isPrototypeOf(subKlass));
console.log(SuperKlass.prototype.isPrototypeOf(SubKlass.prototype));
// true true true

How does the lookup work for each of these? For example, the definition of the method states:

The isPrototypeOf() method checks if an object exists in another object's prototype chain.

So for the first one, SuperKlass.isPrototypeOf(SubKlass), SubKlass is the object whose prototype chain we are inspecting to find if it exists in SuperKlass. So then how does that work exactly? I tried debugging it a big in Chrome dev tools and figured it out on some trial-and-error for the first one:

enter image description here

When looking up the prototype chain does it use __proto__ or .prototype? And how would it work for the other two?

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

0

There are two important concepts here to understand Object.prototype.isPrototypeOf() :

  1. The prototype chain is traversed via .__proto__, not via .prototype.
  2. If the method call has the form: Parent.isPrototypeOf(item), then we first check to see if item.__proto__ === Parent, if it is not then we check to see if item.__proto__.__proto__ === Parent, ... all the way until the prototype chain ends (<...>.__proto__ === null).

With this we can build a basic function to emulate this method and add in some debugging helpers to see how it works:

function isPrototypeOf(obj, parent) {
    // a function to search the prototype chain of object to see if it's found in parent
    let isFound = false;
    let proto = obj.__proto__;
    while (proto) {
        console.log('Proto: ', proto);
        if (proto === parent) {
            console.log('Found!');
            isFound = true;
            break;
        }
        proto = proto.__proto__;
    }
    return isFound;
}


class SuperKlass {};
class SubKlass extends SuperKlass {};
const superKlass = new SuperKlass();
const subKlass = new SubKlass();
console.log(isPrototypeOf(SubKlass, SuperKlass));
console.log(isPrototypeOf(subKlass, SuperKlass.prototype));
console.log(isPrototypeOf(SubKlass.prototype, SuperKlass.prototype));
console.log(isPrototypeOf(SubKlass.prototype, SuperKlass.prototype.prototype)); // this shouldn't be found and will traverse until null

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