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

435
Visualizações
Comparing Custom Strings(classes extending String class) using ==

I was wondering, if there are any ways on how to compare 2 custom string objects which are instances of classes which extend String, using == doesn't work and it always returns false.

I have tried using valueOf but with no luck, the method gets never called if both operands are objects.

See an example below

class MyString extends String {
  doSomething(someData) {
    return doSomethingWithData(this, someData);
  };

  valueOf() {
    console.log('valueOf called');
    return this.toString();
  };
};

When doing console.log(new MyString('hi') == 'hi');, I Get

valueOf called
true

But When doing console.log(new MyString('hi') == new MyString('hi'));, I Get false.

So, the valueOf gets never called and the comparison blindly returns false, is there any possible way to fix it?

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