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
Argument type Number is not assignable to parameter type String|Function
提问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 replace
method 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 replace
wants a string. Just use:
是一个数值并且需要replace
一个字符串。只需使用:
(temp - temp_integer)+40+""
assuming that you want the string representation of the number (eg, 65
becomes "65"
). If you want the characterat that code point (65
becomes "A"
), you should look into using String.fromCharCode()
.
假设您想要数字的字符串表示形式(例如,65
变成"65"
)。如果您想要该代码点处的字符(65
变成"A"
),您应该考虑使用String.fromCharCode()
.