zhaochangbo

参数一:

参数二:

 

格式化:

 

 

 问题:如果要有 多个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"

 

 

 

分类:

技术点:

相关文章:

  • 2021-09-27
  • 2021-02-07
  • 2021-09-13
  • 2021-12-28
  • 2021-06-11
  • 2021-11-19
猜你喜欢
  • 2021-10-18
  • 2021-11-27
  • 2021-11-27
  • 2021-09-13
  • 2021-08-24
相关资源
相似解决方案