字符串格式化
String.prototype.format = function(args) {
var result = this;
if (arguments.length < 1) {
return result;
}
var data = arguments;
if (arguments.length == 1 && typeof (args) == "object") {
data = args;
}
for (var key in data) {
var value = data[key];
if (undefined != value) {
result = result.replace("{" + key + "}", value);
}
}
return result;
}
使用
var someString = 'a : {a}, b : {b}';
var trueStr = someString.format({'a' : '123', 'b' : '456'});
trueStr的值为 'a : 123, b : 456';