在单个数组对象上重写toString()Javascript [英] Override toString() Javascript On a Single Array Object

查看:75
本文介绍了在单个数组对象上重写toString()Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

var version = [0,3,0];

// Override the version toString method.
version.__proto__.toString = function() {
    return this.join('.');
};

执行以下操作

version.toString => '0.3.0'

JSlint抱怨说__proto__是保留名称-正确.

JSlint moans that __proto__ is a reserved name - which is correct.

我认为我不正确地超载.

I assume I am overloading incorrectly.

我不想

Array.prototype.toString

因为它将覆盖所有数组以将.替换为.?

as that'll override all arrays to replace , with .?

推荐答案

只需直接在数组上设置方法:

Just set the method on the array directly:

var version = [0,3,0];

// Override the version toString method.
version.toString = function() {
    return this.join('.');
};

这篇关于在单个数组对象上重写toString()Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆