Returns a string representation of an object.
toLocaleString(locale?)
locale optional
A string describing which locale to use.
A string representation of the object.
{}.toLocaleString()
"[object Object]"
let vehicle = {
speed: 10,
unit: "km/h",
toString() {
return `${this.speed} ${this.unit}`
},
toLocaleString(locale) {
if (locale == "en-US")
return `${(this.speed / 1.609344).toFixed(1)} mph`
else
return this.toString()
}
}
vehicle.toLocaleString("en-US")
"6.2 mph"