javascript - Object.toString() 与 Object.toLocaleString() 的区别◔ ‸◔?

查看:87
本文介绍了javascript - Object.toString() 与 Object.toLocaleString() 的区别◔ ‸◔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

先上代码

//当对象直接调用这两个方法的时候,他们的返回结果是一样的
//结果都是 [object Object]
var o = {};
console.log(o.toString());//=>[object Object]
console.log(o.toLocaleString());//=>[object Object]

//但是当以call()的方式调用的时候,他们的返回结果发生了改变
function foo () {
  return true;
}
console.log(Object.prototype.toLocaleString.call(foo));//foo函数转换成了字符串
console.log(Object.prototype.toString.call(foo));//=>[object Function]

//而直接用foo调用toString()时,和通过call()调用Object.prototype.toLocaleString()返回的结果一样
console.log(foo.toString());//foo函数转换成了字符串

请问哪位能帮忙讲解下,这其中的原理?
为什么同样是调用Object.prototype对象的toLocaleString()和toString(),用在对象上和函数上却这样的差异?

解决方案


你看有一个被重写了。

这篇关于javascript - Object.toString() 与 Object.toLocaleString() 的区别◔ ‸◔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆