1
我试图获取一个数组对象并更改它的打印表示 - 只是该对象,而不是程序中的所有数组。我希望设置的toString属性将做的工作,但它并不:忽略JavaScript数组的打印表示
var a = [1, 2, 3]
// Prints using the default representation
console.log(a)
// Try to override toString
a.toString = function() {
return 'some new representation'
}
// Still uses the default representation
console.log(a)
我缺少什么?
'console.log()'代码做它想做的事情。当数组被强制为字符串值时,覆盖'.toString()'将起作用,但除非强制执行,否则'console.log()'不一定会这样做。 – Pointy
Duplicate http://stackoverflow.com/questions/9943257/how-do-i-override-the-default-output-of-an-object – mplungjan
检查链接[链接](http://stackoverflow.com/问题/ 6307514/is-it-it-it-it-over-javascript-tostring-function-to-provide-meaningfu),你可以看到如何做到这一点; –