【问题标题】:Why String.Prototype replace doesn't work inside nested functions?为什么 String.Prototype 替换在嵌套函数中不起作用?
【发布时间】:2015-12-08 05:00:28
【问题描述】:

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

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

如果我在主脚本文件中使用此函数(例如在其声明之后),它可以与字符串输出一起正常工作。

如果我在嵌套函数中使用这个函数,更准确地说,我在另一个函数中有一个函数,我在第二个函数中调用“replaceAt”,它不起作用,它会截断“中指定的“索引”之后的所有字符替换“。我还指定这是 Chrome 扩展程序中的内容脚本。

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

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

示例(截断“索引”之后的所有内容):

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);}
 }
}

上面将创建一个复选框输入列表,当用户选中/取消选中一个框时,相应的索引(对应于列表中的输入)将更改为“0”或“1”中的“真/假” temp”,它将在 cookie 中被覆盖。因此,“nfu_change”会根据复选框状态的变化有条件地调用。

【问题讨论】:

  • 如果您可以将其复制为一个易于运行的最小示例,可能会更清楚地看到正在发生的事情,也许使用站点堆栈片段功能。
  • 请发布您的完整代码。这可能是一个上下文问题。
  • nfu_change() 怎么称呼? 什么是i'000000000000000'.replaceAt(i, "1")temp 中为您提供了什么(和/或console.log(i + " - " + temp); 向您展示了什么)?哪个.replaceAt 给您带来了问题?就目前而言,我认为这里没有任何问题。
  • 另外,i 是正数还是负数?
  • @RocketHazmat ,“i”总是积极的。第一个 console.log 在初始化时显示了 15 位全零的“temp”,但在替换操作之后,索引位置之后的所有内容都被截断。

标签: javascript string replace string-split


【解决方案1】:

我的假设是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

【讨论】:

  • 除非他使用.call.apply,否则this 将是调用replaceAt 的字符串。
  • 或者如果嵌套函数是从回调中调用的,'this'不会指向函数。
  • 我不知道 OP 如何调用 nfu_change(),但如果 index 是一个字符串,它看起来确实是个问题。
  • 在这一点上,我很确定this 不是问题所在。听起来他没有使用任何可能导致 this 混淆行为的代码,我通过使用字符串而不是数字准确地重现了这个问题。
  • @mark:thisinside String.prototype.replaceAt 函数。 this 基于特定函数的调用方式。除非他做的不是'string'.replaceAt()temp.replaceAt(),否则这不是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多