gpt4 book ai didi

javascript - 何时使用或不使用 toLocale 方法?

转载 作者:行者123 更新时间:2023-11-29 10:33:53 25 4
gpt4 key购买 nike

下面的 TLDR

我在 MDN 上阅读了 JavaScript 的标准内置对象部分,并注意到有这些方法利用了“Locale”,并且据我所知专门用于格式化方法返回的文本本地定义的格式(如果存在)。貌似火鸡出问题了(不知道有没有其他的)

据我所知,根据我的调查,这些都是在 2011 年左右的 ES 5.1 中实现的。事实上,在下面的引用资料中的一个 SO 链接中,它积极指出了 Angular 为什么1.x 可能使用 toString 而不是 toLocaleString 是因为向后兼容尚未完全采用 ES5.1 的浏览器——简单地说:我不知道情况是否如此,但这似乎是合理的。

所以我查阅了 ES6 规范来检查方法:

对象上:

15.2.4.3 Object.prototype.toLocaleString ( ) This function returns the result of calling toString(). NOTE This function is provided to give all Objects a generic toLocaleString interface, even though not all may use it. Currently, Array, Number, and Date provide their own locale-sensitive toLocaleString methods. NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.

数组上:

15.4.4.3 Array.prototype.toLocaleString ( ) The elements of the array are converted to strings using their toLocaleString methods, and these strings are then concatenated, separated by occurrences of a separator string that has been derived in an implementationdefined locale-specific way. The result of calling this function is intended to be analogous to the result of toString, except that the result of this function is intended to be locale-specific.

字符串上:

15.5.4.17 String.prototype.toLocaleLowerCase ( ) This function works exactly the same as toLowerCase except that its result is intended to yield the correct result for the host environment’s current locale, rather than a locale-independent result. There will only be a difference in the few cases (such as Turkish) where the rules for that language conflict with the regular Unicode case mappings. NOTE The toLocaleLowerCase function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method. NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.

为清晰起见,原始的 ToLowerCase:

15.5.4.16 String.prototype.toLowerCase ( ) If this object is not already a string, it is converted to a string. The characters in that string are converted one by one to lower case. The result is a string value, not a String object. The characters are converted one by one. The result of each conversion is the original character, unless that character has a Unicode lowercase equivalent, in which case the lowercase equivalent is used instead. NOTE The result should 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). NOTE The toLowerCase function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method

(toLocaleUpperCase/toUpperCase读起来完全一样)

鉴于所有这些,随着 ES6 的发布和它被广泛采用,我感到困惑。我觉得 toLowerCase 和 toUpperCase 通常用于验证目的(尽管在 ES6 中使用较少)并且将它们更改为使用 Locale 似乎是错误的,因为您将检查未知格式。所以好吧,对于验证来说并不是很有用。那么使用 toLocaleString 输出到 DOM 呢?这似乎是合理的,但同样,你正在处理未知数。假设您的语言环境未格式化并且您希望整数 1000 显示为“1,000”。 (我读过 en-GB 会发生这种情况)它会让它脱离您的控制,您甚至可能永远不会知道它没有按照您想要的方式显示。

TLDR:toLocaleString toLocaleLowerCase toLocaleUpperCase 等方法是否有实际用例?他们应该在很大程度上被忽略吗?

注意:我知道这是自以为是的做法,但我不认为是这样。我正在寻找合理的案例,如果它们存在,这些案例可能适用。例如比如问“你会用 .call 做什么”而不是“你认为 .call 比 .apply 好吗”

引用资料

MDN String Prototype: toLocaleLowerCase

SO: Difference Between toLocaleLowerCase and toLowerCase

SO: In which exactly js engines are toLowerCase toUpperCase locale sensitive

SO: JavaScript difference between toString and toLocaleString methods of date

最佳答案

It seems plausible, but again, you're dealing with unknowns.

是的。您需要了解他们。

It will leave it out of your hands and you may never even know that it's not displaying as you wanted it to.

的确如此。如果你想/需要完全控制你的输出,你需要自己实现格式。如果您只是说,嘿,这是一个数字,请将其格式化为您认为最适合英国语言环境的格式,然后您就可以使用它了。

Is there a practical use case for methods like toLocaleString etc.?

是的!您将希望在支持 ECMA-402 Standard 的环境中使用它们.

来自 the API Overview :

"ECMAScript 2016 国际化 API 规范提供了大多数应用程序所需的几个关键的语言敏感功能:字符串比较(整理)、数字格式化、日期和时间格式化以及大小写转换。虽然 ECMAScript 2016 Language Specification 为这个基本功能提供了函数(在 Array.prototype 上:toLocaleString;在 String.prototype 上:localeComparetoLocaleLowerCasetoLocaleUpperCaseNumber.prototype:toLocaleStringDate.prototype: toLocaleStringtoLocaleDateStringtoLocaleTimeString),它让这些函数的实际行为很大程度上取决于实现来定义。ECMAScript 2016国际化 API 规范提供了额外的功能、对语言和行为细节的控制使用,以及所需功能的更完整规范。

Should they be largely ignored?

可能是在未知环境中。但当您知道它们的作用时(因为您控制了环境,或者您希望它符合 ECMA-402),就不是这样了,因为在这些情况下它们会非常有用并且可以减轻您的大量工作。

关于javascript - 何时使用或不使用 toLocale 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40028179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com