简体   繁体   English

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

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

I have declared in same script file the following sub-string replace function :我在同一个脚本文件中声明了以下子字符串替换函数:

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

If I use this function in the main script file (for example right after its declaration), it works properly with string output.如果我在主脚本文件中使用这个函数(例如在它声明之后),它可以正常处理字符串输出。

If I use this function inside nested functions, more exactly I have a function inside another function and I call "replaceAt" inside the second function, it doesn't work and it truncates all characters after the "index" specified in "replaceAt".如果我在嵌套函数中使用这个函数,更确切地说,我在另一个函数中有一个函数,我在第二个函数中调用了“replaceAt”,它不起作用,它会截断“replaceAt”中指定的“索引”之后的所有字符。 I also specify that this is a content script in a Chrome extension.我还指定这是 Chrome 扩展程序中的内容脚本。

Example (works okay outside functions, in main file) :示例(在函数外,在主文件中工作正常):

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

Example (truncates everything after "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);}
 }
}

The above will create a list of checkbox inputs and when user checks/unchecks a box, the respective index (corresponding to the input in the list) will be changed to either "0" or "1" true/false in "temp" which will be overwritten in a cookie.以上将创建一个复选框输入列表,当用户选中/取消选中一个框时,相应的索引(对应于列表中的输入)将在“temp”中更改为“0”或“1”真/假将在 cookie 中被覆盖。 So, "nfu_change" is called conditionally on change of checkbox status.因此,“nfu_change”根据复选框状态的变化被有条件地调用。

My hypothesis is that this doesn't mean what you think it means.我的假设是, this并不意味着你认为它意味着什么。 Run your code thru a debugger and see what value this takes on in the places where your function does and does not do what you expect.运行代码直通一个调试器,看看有什么价值this发生在当你的函数不和你所期望的没有做的地方。


Edit I think that the above hypothesis is wrong.编辑我认为上述假设是错误的。

I was able to reproduce your problem by calling your replaceAt function with a string for the index.通过使用索引字符串调用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

Here's the solution:这是解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM