我有一个generateRandomArray函数,如果参数不是数组,它应该停止函数执行并将错误输出到控制台,而不打印传递的参数。如果参数是数组,它应该继续。然而,上面的代码不起作用。
<script>
function generateRandomArray(array){
if(typeof array != 'array'){
console.log("Uncaught typeError: the argument is not a array");
}
var storage = Math.floor(Math.random() * array.length);
return array[storage];
}
var arrayer = generateRandomArray('d');
console.log(arrayer );
</script>我尝试了return;,以停止函数执行,但它不起作用。
发布于 2021-06-26 07:11:21
这个问题的最佳解决方案是throw new Error("one error"),就像said blex一样。但是如果你在有用户的时候停止执行你的程序,这对你是没有好处的……因此,这就是为什么您应该使用console.error("another error")
https://stackoverflow.com/questions/68137816
复制相似问题