为 javascript 的 String 对象增加一个 format(args[])方法。
此方法可以将字符串中的{0},{1},{2}等占位符替换为参数传入的值。
如: var msg = 'The value of {0} must be less than {1}.'.format(['score', '100']);
msg 的值为 The value of score must be less than 100.
此方法可以将字符串中的{0},{1},{2}等占位符替换为参数传入的值。
如: var msg = 'The value of {0} must be less than {1}.'.format(['score', '100']);
msg 的值为 The value of score must be less than 100.
var arg = arguments;
return this.replace(/\{(\d+)\}/g, function() {
return arg[+arguments[1]];
});
}var msg = 'The value of {0} must be less than {1}.'.format(['score', '100']);
alert(msg);
var msg = 'The value of {0} must be less than {1}.'.format('score', '100');
#3楼 得分:0回复于:2010-03-16 12:54:26bug