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

197
Visualizações
Basic javascript question about call() function: Conversion of non-string this values to strings

Reading MDN docs and have found this code:

const a = String.prototype.toUpperCase.call({
  toString: function toString() {
    return 'abcdef';
  }
});
// prints out 'ABCDEF'

Function call takes as first argument object with method toString, which means calling a function toUpperCase() on object or (correct me please if I'm wrong):

({ toString: function toString() { return 'abcdef'; }}).toUpperCase()

This code prints out 'ABCDEF', so how could toUpperCase() method invoke function toString() inside object that's what I don't understand.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're calling toUpperCase passing it a this value that's an object with a toString method. Contrary to popular belief, toUpperCase doesn't assume this will be a string. Instead, it converts whatever this is (in your case, the object with a toString method) to a primitive string as one of its first steps (see the spec here¹). Doing that calls the toString method of your object (details here and here). At that point, toUpperCase has "abcdef" to work with, converts it to upper case, and returns it.

Conceptually (not exactly), toUpperCase does this:

function toUpperCase() {
    const S = String(this); // This is what triggers the call to `toString`
                            // on your object
    const L = /*...convert the characters in S to to upper case*/;
    return L;
}

¹ That's for toLowerCase, but toUpperCase is defined by saying "This function behaves in exactly the same way as String.prototype.toLowerCase, except that the String is mapped using the toUppercase algorithm of the Unicode Default Case Conversion."

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