为什么 String.Prototype 替换在嵌套函数中不起作用?

Why String.Prototype replace doesn't work inside nested functions?

我在同一个脚本文件中声明了以下子字符串替换函数:

String.prototype.replaceAt = function(index, character) {
    index = parseInt(index, 10);
    return this.substr(0, index) + character + this.substr(index + character.length);
}

如果我在主脚本文件中使用这个函数(例如在它的声明之后),它可以正确地处理字符串输出。

如果我在嵌套函数中使用这个函数,更确切地说,我在另一个函数中有一个函数,我在第二个函数中调用 "replaceAt",它不起作用,它会截断 [= 之后的所有字符27=] 在 "replaceAt" 中指定。我还指定这是 Chrome 扩展中的内容脚本。

示例(在函数外部工作正常,在主文件中):

var h = '000000';
h = h.replaceAt(3, "1");
console.log(h);

示例(截断 "index" 之后的所有内容):

function do_lut() {
    temp = '000000000000000';

    function nfu_change(e, i) {
        if (e.checked) {
            if (temp != null) {
                console.log(i + " - " + temp);
                temp = temp.replaceAt(i, "1");
            } else { temp = '000000000000000'.replaceAt(i, "1"); } 
                       }
        else { if(temp!=null) {temp = temp.replaceAt(i,"0");} else {temp = new String('000000000000000');} }
        console.log(i+" - "+temp);
        }
    }

 for(i=0;i<15;i++) 
 {
  nfu[i] = document.createElement('INPUT');
  nfu[i].type = 'checkbox';
  nfu[i].id = Math.floor((Math.random() * 100000000) + 1).toString();
  nfu[i].name = i.toString();
  nfu[i].onchange = function(e) {nfu_change(e.target,e.target.name);}
 }
}

以上将创建一个复选框输入列表,当用户 checks/unchecks 一个框时,相应的索引(对应于列表中的输入)将更改为“0”或“1”true/false 在 "temp" 中,这将在 cookie 中被覆盖。因此,"nfu_change" 在复选框状态发生变化时有条件地调用。

我的假设是 this 并不像您认为的那样。 运行 通过调试器检查您的代码,并查看 this 在您的函数执行和不执行您期望的地方的值。


编辑我认为上述假设是错误的

通过使用索引字符串调用您的 replaceAt 函数,我能够重现您的问题。

String.prototype.replaceAt = function (index, character) {
    return this.substr(0, index) + character + this.substr(index +  character.length);
}

alert("abc".replaceAt(1, "B")); // aBc
alert("abc".replaceAt("1", "B")); //aB

解决方法如下:

String.prototype.replaceAt = function (index, character) {
    index = parseInt(index, 10);
    return this.substr(0, index) + character + this.substr(index + character.length);
}

alert("abc".replaceAt(1, "B")); // aBc
alert("abc".replaceAt("1", "B")); //aBc