在JS引擎中,具体来说,是针对低容量的& toUpperCase语言环境敏感? [英] In what JS engines, specifically, are toLowerCase & toUpperCase locale-sensitive?
问题描述
在某些图书馆的代码中(例如 AngularJS ,链接指向代码中的特定行),我可以看到使用自定义大小写转换函数而不是标准函数。假设在具有土耳其语语言环境的浏览器中,标准函数不能按预期工作,这是合理的:
In the code of some libraries (e.g. AngularJS, the link leads to the specific lines in the code), I can see that custom case-conversion functions are used instead of the standard ones. It's justified by an assumption that in browsers with Turkish locale, the standard functions don't work as expected:
console.log("SCRIPT".toLowerCase()); // "scrıpt"
console.log("script".toUpperCase()); // "SCRİPT"
但它是真的还是曾经如此?浏览器真的有这样的表现吗?如果是这样,他们中的哪一个呢? node.js怎么样?其他JS引擎?
But is it really or was it ever the case? Do the browsers really behave this way? If so, which of them do? What about node.js? Other JS engines?
toLocaleLowerCase
和 toLocaleUpperCase $ c $的存在c>方法暗示
toLowerCase
和 toUpperCase
是locale-invariant,不是吗?
The existance of the toLocaleLowerCase
and toLocaleUpperCase
methods implies that toLowerCase
and toUpperCase
are locale-invariant, doesn't it?
对于什么浏览器,具体来说,Angular团队在代码中保留了这个检查: if('i'!=='I'.toLowerCase()).. 。
?
For what browsers, specifically, does the Angular team retain this check in the code: if ('i' !== 'I'.toLowerCase())...
?
如果您的浏览器(设备)使用土耳其语或阿塞拜疆语语言环境,请运行如果您发现问题确实存在,请写下这个片段。
If your browser (device) uses the Turkish or Azerbaijan locale, please run this snippet and write me if you discover that the issue indeed exists.
if ('i' !== 'I'.toLowerCase()) {
document.write('Ooops! toLowerCase is locale-sensitive in your browser. ' +
'Please write your user-agent in the comments to this question: ' +
navigator.userAgent);
} else {
document.write('toLowerCase isn\'t locale-sensitive in your browser. ' +
'Everything works as expected!');
}
<html lang="tr">
推荐答案
遵循ECMA-262 5.1标准的任何JS实现必须实现 String.prototype .toLocaleLowerCase
和 String.prototype.toLocaleUpperCase
Any JS implementations that follow ECMA-262 5.1 standard have to implement String.prototype.toLocaleLowerCase
and String.prototype.toLocaleUpperCase
按照标准 toLocaleLowerCase
应该根据特定于语言环境的映射将字符串转换为小写映射。
And as per the standard toLocaleLowerCase
is supposed convert string to it's lower case mapping as per locale specific mapping.
其中 toLowerCase
转换为unicode映射定义的小写字符串。
Where as toLowerCase
converts to lowercase string as defined by unicode mappings.
对于大多数语言 toLocaleLowerCase
和 toLowerCase
给出相同的结果。但是对于某些语言,例如turkish,case映射不遵循unicode映射,因此 toLowerCase
和 toLocaleLowerCase
给出不同的结果。
For most languages toLocaleLowerCase
and toLowerCase
give the same result. But for certain languages, like turkish the case mapping don't follow unicode mapping thus toLowerCase
and toLocaleLowerCase
give different result.
您使用的库/框架(Jquery,Angular,Node等)无论如何都没有任何区别。它是用于运行JS库的JS实现,用于创建和更改内容。
The Library/Framework you use (Jquery, Angular, Node whatever else) does not make any difference whatsoever. It's in what JS implementation you use to run your JS libaries that makes and changes things.
出于所有实际目的,可以准确地得出结论,Node / Angular或任何其他JS库和框架在处理字符串时都表现得完全一样(只要它们被使用)由JS Engine实现ECMA-262 3及更高版本)。话虽如此,我确信很多框架都会扩展字符串对象以添加更多功能,但 ECMA-262 5.1定义的基本属性和函数始终存在且WILL的行为完全相同。
For all practical purposes it's accurate to conclude that Node/Angular or any other JS libraries and frameworks all behave exactly the same when dealing with strings (as long as they are used by JS Engine that implements ECMA-262 3 and above). Having said that, I'm sure many frameworks extend the string object to add more functionality, but the basic properties and functions defined by ECMA-262 5.1 always exists and WILL behave exactly the same.
要了解更多信息:http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.17
就浏览器而言,所有现代浏览器都在其JS引擎中实施ECMA-262 5.1标准。我不确定Node,但是我对Node的有限曝光,我认为他们也使用按照ECMA-262 5.1标准实现的JS。
As far as browsers are concerned, all modern browsers implement the ECMA-262 5.1 standards in their JS engine. I am not sure about Node, but from what limited exposure I have with Node, I think they too use JS implemented per ECMA-262 5.1 standard.
这篇关于在JS引擎中,具体来说,是针对低容量的& toUpperCase语言环境敏感?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!