var a = "dragon {num},{age}"; var b = "dragon {0},{1}"; String.prototype.format = function (){ var str = this.toString(); if(typeof arguments[0] === "object"){ for(i in arguments[0]){ str = str.replace("{"+i+"}",arguments[0][i]); } }else{ for(var i=0; i<arguments.length; i++){ str = str.replace("{"+i+"}",arguments[i]) } } return str; }; console.log(a.format({num:10,age:100})); console.log(b.format('10','100'));