什么是toLocaleString()?

Ran*_*lue 5 javascript google-chrome

根据这个MDN页面,toLocaleString是关于转换日期.但是,Chrome会在多个字符串上公开该功能.例如:

a = function () {};
a.toLocaleString();  // "function () {}"
Run Code Online (Sandbox Code Playgroud)

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

pim*_*vdb 5

它也是可用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
}
Run Code Online (Sandbox Code Playgroud)