Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“引用时如何更改javascript字符串值”;这";_Javascript - Fatal编程技术网

“引用时如何更改javascript字符串值”;这";

“引用时如何更改javascript字符串值”;这";,javascript,Javascript,当“this”指的是javascript字符串对象时,有没有办法更改“this”的值 抱怨非法的左侧作业 编辑:为了澄清,我正在尝试以下内容,但似乎没有办法做到这一点 String.prototype.replaceAll = function (find, replace) { while (this.indexOf(find) > -1) { this = this.replace(find, replace); // illegal } } 没有 您

当“this”指的是javascript字符串对象时,有没有办法更改“this”的值

抱怨非法的左侧作业

编辑:为了澄清,我正在尝试以下内容,但似乎没有办法做到这一点

String.prototype.replaceAll = function (find, replace) {
    while (this.indexOf(find) > -1) {
        this = this.replace(find, replace); // illegal
    }
}
没有

您永远不能将值赋给此,并且字符串是不可变的


如果要覆盖为
提供上下文的变量的值,则必须按名称访问该变量。

正如错误所述,您不能更改
的值(即分配给它)。您不能更改
的等效值。(MDN)正在谈论it相关:仅供参考:这看起来可以通过
返回此.replace(new RegExp(find,'g'),replace);
谢谢Felix。这很有帮助。
String.prototype.replaceAll = function (find, replace) {
    while (this.indexOf(find) > -1) {
        this = this.replace(find, replace); // illegal
    }
}