Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.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 在js中更改函数变量的类型_Javascript - Fatal编程技术网

Javascript 在js中更改函数变量的类型

Javascript 在js中更改函数变量的类型,javascript,Javascript,我想强制函数变量类型为字符串 function removeFirstCharacter (element) { var el = (element).toString(); return el.slice(1); } 移除FirstCharacter0011->返回而不是011。 我不知道为什么0011被转换成空字符串。我的目标是,当我输入一个数字时,该数字应转换为字符串。对于字符串参数,该代码工作正常。数字的问题只是我的问题。任何指导都将不胜感激。谢

我想强制函数变量类型为字符串

function removeFirstCharacter (element) {
        var el = (element).toString();
        return el.slice(1);
    }
移除FirstCharacter0011->返回而不是011。 我不知道为什么0011被转换成空字符串。我的目标是,当我输入一个数字时,该数字应转换为字符串。对于字符串参数,该代码工作正常。数字的问题只是我的问题。任何指导都将不胜感激。谢谢

移除FirstCharacter0011->返回而不是011。我不知道为什么0011被转换成空字符串

数字的前导零告诉javascript将其视为八进制。0011在十进制中表示9,因此removeFirstCharacter0011实际上是removeFirstCharacter9。当你把它变成一个字符串,你得到9,当你删除第一个字符,你得到一个空字符串

移除FirstCharacter0011->返回而不是011。我不知道为什么0011被转换成空字符串


数字的前导零告诉javascript将其视为八进制。0011在十进制中表示9,因此removeFirstCharacter0011实际上是removeFirstCharacter9。当您将其转换为字符串时,您得到9,当您删除第一个字符时,您得到一个空字符串。

在您的元素处删除

函数removeFirstCharacter元素{ var el=element.toString; 返回el.1; } console.logremoveFirstCharacter123;
console.logremoveFirstCharacter123 在你的元素中移除

函数removeFirstCharacter元素{ var el=element.toString; 返回el.1; } console.logremoveFirstCharacter123;
console.logremoveFirstCharacter123 您提供的输入数字不是十进制的,当它被转换为十进制时,0011等于9,正如Nicholas上面所说的。因此,您需要将所有数字输入解析为十进制,如下面的console.log所示

绝不允许将输入数据视为“数字”-在调用removeFirstCharacter时,输入数据应始终保持为字符串,直到解析为int

function removeFirstCharacter (element) {
  return element.toString().slice(1);
}

console.log(removeFirstCharacter(parseInt('0011', 10)));

您提供的输入数字不是十进制的,当它被转换为十进制时,0011等于9,正如Nicholas上面所说的。因此,您需要将所有数字输入解析为十进制,如下面的console.log所示

绝不允许将输入数据视为“数字”-在调用removeFirstCharacter时,输入数据应始终保持为字符串,直到解析为int

function removeFirstCharacter (element) {
  return element.toString().slice(1);
}

console.log(removeFirstCharacter(parseInt('0011', 10)));

您想调用removeFirstCharacter0011而不是removeFirstCharacter0011您想调用removeFirstCharacter0011而不是removeFirstCharacter0011,但执行此操作时仍然失败:console.logremoveFirstCharacter0123;但当我这样做时,它仍然失败:console.logremoveFirstCharacter0123;