问题
I understand that using that function will make an entire string all lowercase. However, I'm curious in the behind the scenes work. I can't find an explanation anywhere on how it works. Does it basically loop through every index in the string and check to see what the character is and if there is a lower case character available?
回答1:
In general the best place to look for such information is the ECMAScript specification:
The following steps are taken:
- Call CheckObjectCoercible passing the this value as its argument.
- Let S be the result of calling ToString, giving it the this value as its argument.
- Let L be a String where each character of L is either the Unicode lowercase equivalent of the corresponding character of S or the actual corresponding character of S if no Unicode lowercase equivalent exists.
- Return L.
For the purposes of this operation, the 16-bit code units of the Strings are treated as code points in the Unicode Basic Multilingual Plane. Surrogate code points are directly transferred from S to L without any mapping.
The result must be derived according to the case mappings in the Unicode character database (this explicitly includes not only the UnicodeData.txt file, but also the SpecialCasings.txt file that accompanies it in Unicode 2.1.8 and later).
Step 3 is the part you're really interested in. As you can see, the details of how "L" is produced are up to the implementation. If you're interested in going deeper the next place to look would be e.g. the V8 engine itself.
来源:https://stackoverflow.com/questions/30596945/how-does-tolowercase-work-in-javascript