Javascript:string';s格式未定义
我有以下javascript代码片段:Javascript:string';s格式未定义,javascript,Javascript,我有以下javascript代码片段: var someValue = 100; var anotherValue = 555; alert('someValue is {0} and anotherValue is {1}'.format(someValue, anotherValue)); 获取以下错误: Uncaught TypeError: undefined is not a function 我错过了什么,在这里 字符串。格式不是本机的字符串扩展名。自己扩展它很容易: if(!
var someValue = 100;
var anotherValue = 555;
alert('someValue is {0} and anotherValue is {1}'.format(someValue, anotherValue));
获取以下错误:
Uncaught TypeError: undefined is not a function
我错过了什么,在这里
字符串。格式
不是本机的字符串
扩展名。自己扩展它很容易:
if(!String.prototype.format){
String.prototype.format=函数(…args){
返回这个。replace(/(\{\d+\})/g,函数(a){
返回args[+(a.substr(1,a.length-2))| | 0];
});
};
}
//用法
log(“{0}world.format”(“hello”)代码>
.as控制台包装{top:0;最大高度:100%!important;}
String.format=function(){
var s=参数[0];
对于(变量i=0;i
Javascript中没有本机字符串格式方法。见:,
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i += 1) {
var reg = new RegExp('\\{' + i + '\\}', 'gm');
s = s.replace(reg, arguments[i + 1]);
}
return s;
};
var strTempleate = String.format('hello {0}', 'Ortal');