Symbol.replace
ymbol.replace
这个属性指定了当一个字符串替换所匹配字符串时所调用的方法。
String.prototype.replace()
方法会调用此方法。
Symbol.replace
属性的属性特性:
|
|
---|---|
writable | false |
enumerable | false |
configurable | false |
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"