• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • Symbol.replace

    ymbol.replace这个属性指定了当一个字符串替换所匹配字符串时所调用的方法。String.prototype.replace()方法会调用此方法。


    Symbol.replace属性的属性特性:
    writablefalse
    enumerablefalse
    configurablefalse


    class Replace1 {
      constructor(value) {
        this.value = value;
      }
      [Symbol.replace](string) {
        return `s/${string}/${this.value}/g`;
      }
    }
    
    console.log('foo'.replace(new Replace1('bar')));
    // expected output: "s/foo/bar/g"
    

    上篇:Symbol.matchAll

    下篇:Symbol.search