前段 format方法

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)
posted @ 2017-08-04 19:40  karina梅梅  阅读(165)  评论(0编辑  收藏  举报