javascript中,没有类的概念。要重写Object的toString方法,可以通过其prototype去实现。比如,自己定义了一个数组类,重写toString过程如下:
MyArray.prototype = {
//重写toString方法
toString : function() {
var str = "";
for(var i=0; i<this.length-1; i++) {
str += this[i] + ",";
}
str += this[this.length-1];
return str;
}
}