javascript 参数类型 Number 不能分配给参数类型 String|Function

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17355704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:00:25  来源:igfitidea点击:

Argument type Number is not assignable to parameter type String|Function

javascriptwebstorm

提问by Chris

var str = name.toUpperCase();
var ch = new Array();
ch = str.split('');

for(var i=0;i<7;i++)
{
  if(ch = null) {
    result_code.replace(
      pos.toString()+pos.toString()+pos.toString()+pos.toString(),
      "FFFF");
  } else {
    var temp = parseInt(ch[i]);
    var temp_integer = 64;
    if(temp<=122 & temp>=97) {
      var pos = i+1;
      result_code.replace(
        pos.toString()+pos.toString()+pos.toString()+pos.toString(),
        (temp - temp_integer)+40);
    }
  }
}

This code is creating the error at this line result_code.replace(pos.toString()+pos.toString()+pos.toString()+pos.toString(), (temp - temp_integer)+40);.

这段代码在这一行产生了错误result_code.replace(pos.toString()+pos.toString()+pos.toString()+pos.toString(), (temp - temp_integer)+40);

The underlined information is this section (temp - temp_integer)+40.

带下划线的信息是这一部分(temp - temp_integer)+40

The error shown is Argument type Number is not assignable to parameter type String|Function.

显示的错误是Argument type Number is not assignable to parameter type String|Function

What is wrong with this code? I am using WebStorm. I am likely just making a dumb mistake. Thanks in advance!

这段代码有什么问题?我正在使用 WebStorm。我可能只是犯了一个愚蠢的错误。提前致谢!

回答by Guffa

The replacemethod accepts a string or a function as second parameter. Turn your value into a string: ((temp - temp_integer)+40).toString().

replace方法接受一个字符串或一个函数作为第二个参数。将您的值转换为字符串:((temp - temp_integer)+40).toString().

回答by paxdiablo

(temp - temp_integer)+40

is a numeric value and replacewants a string. Just use:

是一个数值并且需要replace一个字符串。只需使用:

(temp - temp_integer)+40+""

assuming that you want the string representation of the number (eg, 65becomes "65"). If you want the characterat that code point (65becomes "A"), you should look into using String.fromCharCode().

假设您想要数字的字符串表示形式(例如,65变成"65")。如果您想要该代码点处的字符65变成"A"),您应该考虑使用String.fromCharCode().