toLowerCase() 如何在 Javascript 中工作?
How does toLowerCase() work in Javascript?
我知道使用该函数会使整个字符串全部小写。但是,我对幕后工作很好奇。我在任何地方都找不到关于它是如何工作的解释。它是否基本上遍历字符串中的每个索引并检查字符是什么以及是否有可用的小写字符?
一般来说,查找此类信息的最佳位置是 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).
第 3 步是您真正感兴趣的部分。如您所见,如何生成“L”的细节取决于实现。如果您有兴趣深入了解下一个地方,例如V8 engine 本身。
我知道使用该函数会使整个字符串全部小写。但是,我对幕后工作很好奇。我在任何地方都找不到关于它是如何工作的解释。它是否基本上遍历字符串中的每个索引并检查字符是什么以及是否有可用的小写字符?
一般来说,查找此类信息的最佳位置是 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).
第 3 步是您真正感兴趣的部分。如您所见,如何生成“L”的细节取决于实现。如果您有兴趣深入了解下一个地方,例如V8 engine 本身。