我试图获取一个数组对象并更改其打印表示 - 只是该对象,而不是程序中的所有数组。我希望设置toString属性可以完成这项工作,但它并没有:
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)

我错过了什么?
答案 0 :(得分:0)
var a = [1, 2, 3];
// Prints using the default representation
console.log(a);
// Try to override toString
a.toString = function() {
return 'some new representation'
}
// append blank string to invoke toString function
console.log(""+a);