What is toLocaleString()?

What is toLocaleString()?

本文关键字:toLocaleString is What      更新时间:2023-09-26

根据这个MDN页面,toLocaleString是关于转换日期的。然而,Chrome在多个字符串上公开了该函数。例如:

a = function () {};
a.toLocaleString();  // "function () {}"

什么是toLocaleString?例如,为什么它暴露在空函数上?

它也可以在Object.prototype上使用,所以几乎可以间接地在任何东西上使用。

对于Chrome,你可以看看V8的实现,它没有任何花哨的功能:

function ObjectToLocaleString() {
  if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
    throw MakeTypeError("called_on_null_or_undefined",
                        ["Object.prototype.toLocaleString"]);
  }
  return this.toString();  // <-- just calls toString
}