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

202
Vistas
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 Respuestas
Responde la pregunta

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 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