Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript toString()不';我不能像以前那样工作了_Javascript_String_Function_Tostring - Fatal编程技术网

Javascript toString()不';我不能像以前那样工作了

Javascript toString()不';我不能像以前那样工作了,javascript,string,function,tostring,Javascript,String,Function,Tostring,我有个问题。 我有一个代码,例如: var g = "192"; //Global defined// function g(){ if (g<200){ g.toString(); console.log(typeof(g)); g++; window.setTimeout("rgb()", 3000); } else { return; }} $(g); var g=“1

我有个问题。 我有一个代码,例如:

var g = "192"; //Global defined//

function g(){

    if (g<200){

        g.toString();
        console.log(typeof(g));
        g++;
        window.setTimeout("rgb()", 3000);

    } else {

        return;
    }}
$(g);
var g=“192”//全局定义//
函数g(){
如果(g)值和函数名称相同,
g
,则需要保存新值:
var g=“192”//de全局值
函数green(){
g=g.parseInt(g);//确定它是字符串。

如果(g需要将toString指定给变量,并为函数或变量指定不同的名称,如下所示:

var g = "192"; //Global defined//

function test() {

    if (g < 195) {

        g = g.toString();
        console.log(typeof (g));
        g++;
    } else {

        return;
    }
}
test();
var g=“192”//全局定义//
功能测试(){
如果(g<195){
g=g.toString();
控制台日志(类型(g));
g++;
}否则{
回来
}
}
test();

参见此

Javascript是松散类型的,如果存在强制将字符串作为数字处理的操作,它将尝试将字符串转换为数字

var a = "12345", b = a;    // "Pure" number strings
var c = "12345eee", d = c;    // "Tainted" number strings"
console.log (a, b, c, d);
console.log (typeof a, typeof b, typeof c, typeof d);
a ++;    // Increment, which forces a cast to a number
b += 1;    // Add 1 to the end, adds '1' to the end of string
c ++;    // Increment, forces cast to a number
d += 1;    // Add 1 to the end, adds '1' to the end of string
console.log (a, b, c, d);
console.log (typeof a, typeof b, typeof c, typeof d);
正如您从输出中看到的,当您递增(如a和c)时,它会转换为一个数字,然后递增该数字(从而永久性地将其转换为一个数字)。但是,当您在末尾添加一个数字(如b和d)时,它会获取该数字的字符串并将其添加到该字符串中


您可以在日志中看到c值的递增形式为NaN-这是由于尝试将c转换为数字时出现错误,原因是由于字符。

g++
将其转换为数字。
g.toString();
不做任何操作,因为您没有将其结果分配给任何对象。即使我设置了“toString()”在g++下,它将不起作用。你的意思是给函数赋予与变量相同的名称吗?你在末尾调用的
$
函数在哪里?还要注意,如果g是一个字符串,g++将g转换为一个数字,但不增加它的值。你没有调用
绿色的
函数。@Barmar我从c回来奥菲只是考虑到了这个问题。修正了,谢谢。那只是个错误
var a = "12345", b = a;    // "Pure" number strings
var c = "12345eee", d = c;    // "Tainted" number strings"
console.log (a, b, c, d);
console.log (typeof a, typeof b, typeof c, typeof d);
a ++;    // Increment, which forces a cast to a number
b += 1;    // Add 1 to the end, adds '1' to the end of string
c ++;    // Increment, forces cast to a number
d += 1;    // Add 1 to the end, adds '1' to the end of string
console.log (a, b, c, d);
console.log (typeof a, typeof b, typeof c, typeof d);