Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!