问题
I have the following:
var version = [0,3,0];
// Override the version toString method.
version.__proto__.toString = function() {
return this.join('.');
};
Which does the following
version.toString => '0.3.0'
JSlint moans that __proto__
is a reserved name - which is correct.
I assume I am overloading incorrectly.
I do not want to
Array.prototype.toString
as that'll override all arrays to replace , with .?
回答1:
Just set the method on the array directly:
var version = [0,3,0];
// Override the version toString method.
version.toString = function() {
return this.join('.');
};
来源:https://stackoverflow.com/questions/7901754/override-tostring-javascript-on-a-single-array-object