Override toString() Javascript On a Single Array Object

狂风中的少年 提交于 2019-12-23 19:57:47

问题


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

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