In Javascript, does console.log on an object call toString?

眉间皱痕 提交于 2020-01-06 07:58:08

问题


Array.prototype.toString = function(){
    return "testing";
}

a1 = [1,2,3];

console.log(a1); // returns [1,2,3]
console.log(a1.toString()); // returns "testing"

why is that? How does the first log print out values? I thought it used the toString method?


回答1:


For Firefox(both Firebug and Dev Web Console), Chrome: No

For IE: Yes



来源:https://stackoverflow.com/questions/13244308/in-javascript-does-console-log-on-an-object-call-tostring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!