前段 format方法

时间:2023-01-22 23:52:46

a.为字符串创建format方法,用于字符串格式化

String.prototype.format=function (arg) {
//console.log(this,arg); //this,当前调用方法的字符串,arg为Format方法传入的参数
//return '666'; //return,格式化之后获取的新内容,return啥就替换为啥
var temp = this.replace(/\{(\w+)\}/g,function (k,kk) {
// k相当于{(\w+)},kk相当于(\w+)要替换的东西,arg一般是一个字典
return arg[kk];
});
return temp;
}; #调用
a = {nid}
b = {"nid":"123"}
a.format(b)