function formatString() {
if (arguments.length === 0)
return null;
var str = arguments[0];
for (var i = 1; i < arguments.length; i++) {
var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
str = str.replace(re, arguments[i]);
}
return str;
},
formatString('{0},My name is {1}', 'everyMan', '吴孔云')
var myObject = maker(f,l,m,c,s);//不建议写法
var myObject = maker({//建议写法
first : f,
last: l,
state:s,
city:c
});
function render(template, context) {
//被转义的的分隔符 { 和 } 不应该被渲染,分隔符与变量之间允许有空白字符
var tokenReg = /(\\)?\{([^\{\}\\]+)(\\)?\}/g;
return template.replace(tokenReg, function (word, slash1, token, slash2) {
//如果有转义的\{或\}替换转义字符
if (slash1 || slash2) {
return word.replace('\\', '');
}
// 切割 token ,实现级联的变量也可以展开
var variables = token.replace(/\s/g, '').split('.');
var currentObject = context;
var i, length, variable;
for (i = 0, length = variables.length; i < length; ++i) {
variable = variables[i];
currentObject = currentObject[variable];
// 如果当前索引的对象不存在,则直接返回空字符串。
if (currentObject === undefined || currentObject === null) return '';
}
return currentObject;
})
}
String.prototype.render = function (context) {
return render(this, context);
};
"{greeting}! My name is { author.name }.".render({
greeting: "Hi",
author: {
name: "hsfzxjy"
}
});
// Hi! My name is hsfzxjy.
var a = 'kenny'
`my name is ${a}` //"my name is kenny"
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有