Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda