var myString = "hoge"; myString.toS();
var myArray = new Array("red", "green", "blue");
myArray.toS();
var myObject = {'a':[1,2,3],'b':'hoge','c':33};
myObject.toS();
オブジェクトで入れ子になっている場合
var myObject2 = {'a':{'b':'hoge','c':33}};
myObject2.toS();
var myRegExp = /hoge/g; myRegExp.toS();
Object.prototype.toS = function() {
var con = this.constructor;
if(con == String) {
return '"' + this + '"';
} else if(con == Number) {
return this;
} else if(con == Array) {
var res = '[';
for(var i=0,len=this.length;i<len;i++) {
if(i == len-1)
res += this[i].toS() + ']';
else
res += this[i].toS() + ', ';
}
return res;
} else if(con == RegExp) {
return this;
} else if(con == Object) {
var res = '{';
var i=0;
for(var j in this) {
if(j != 'toS') {
if(i == 0) {
res += j + ':' + this[j].toS(1);
} else {
res += ', ' + j + ':' + this[j].toS(1);
}
i++;
}
}
res += '}';
if(arguments.length) {
return res;
} else {
return '(' + res + ')';
}
}
}