Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is toLocaleString()?

According to this MDN page, toLocaleString is about converting dates. However, Chrome exposes the function on more than strings. For example:

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

What is the toLocaleString? Why is it exposed, for example, on the empty function?

like image 797
Randomblue Avatar asked Aug 28 '12 21:08

Randomblue


1 Answers

It's also available on Object.prototype, so indirectly on pretty much anything.

For Chrome, you can look at V8's implementation, which doesn't do anything fancy:

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
}
like image 55
pimvdb Avatar answered Sep 19 '22 14:09

pimvdb