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

92
Visualizações
Symbol.hasInstance Property working in JS

I understand that Symbol.hasInstance property can be cutomised to check if an object is an instance of a particular class. But what i don't understand is how this is working ? Example:

class Animal {
 static [Symbol.hasInstance](obj){ // how is this obj being passes here on calling the function below
    if(obj.isAnimal) return true
 }
}

const a1 = new Animal();
console.log(a1 instanceOf Animal) // false 
a1.isAnimal = true
console.log(a1 instanceOf Animal) // true 

Question: How is obj being passed to the static function on calling a1 instanceOf Animal

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

There are many times when a function can be called even if you don't have anything in the code that does so explicitly with ()s after the property name. For example, with iterators:

class C {
  *[Symbol.iterator]() {
    for (let i = 0; i < 3; i++) yield i;
  }
}
const c = new C();
for (const item of c) {
  console.log(item);
}

And type coercion:

const obj = {
  toString: () => 'some obj'
};
console.log(String(obj));

And proxies.

const prox = new Proxy({}, {
  has() {
    console.log('has invoked');
    return true;
  }
});
console.log('foo' in prox);

instanceof is just another one of these circumstances.

x instanceof y is specified to first extract the value at the hasInstance symbol from y, if such a value exists:

  1. Let instOfHandler be ? GetMethod(target, @@hasInstance).

and then it'll be invoked with x. If the value doesn't exist,OrdinaryHasInstance will be called with x and y instead. That's all there really is to it - instanceof, by definition, will look up and try to call a Symbol.hasInstance property if there is one.

For almost all operators, you can think of x someOperator y as invoking someOperator with x and y - and the operator often does a number of things with the operands behind the scenes.

about 4 years ago · Santiago Gelvez 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