Hello World

JS字符串格式化

参数一:

参数二:

 

格式化:

 

 

 问题:如果要有 多个name,kwargs 进行替换?

 

引出:  JS原型:为类增加方法,以后所有对象都可调用

js中定义类:

    function Foo(name,age){
        this.name = name;
        this.age = age;
    }
    
    Foo.prototype.func = function(){
        this
    }
    
    obj = new Foo()
    obj.func()

 

  

 整合:

String.prototype.format = function (kwargs) {
    return this.replace(/\{(\w+)\}/g, function (k, v) {
    return kwargs[v]
    });
};


name = "aaa{AAA},bbb{BBB}"
"aaa{AAA},bbb{BBB}"
NewName = name.format({"AAA":123,'BBB':789})
"aaa123,bbb789"

 

 

 

posted @ 2017-10-12 23:09  nayike  阅读(3294)  评论(0)    收藏  举报

Hello