简体   繁体   English

谁能告诉我为什么在尝试调用此函数时会出错?

[英]Can anyone tell me why I get a error when i try to call this function?

<!doctype html>
<html>
<script>
function isNumber(value) {
    return typeof (value) != "boolean" && !isNaN(value) && value.length > 0;
}

function minMaxDefrost(value, min, max) {

    if (value.length == 0 || value == "-") return value;
    if (!isNumber(value)) return value.substring(0, value.length - 1);
    
console.log("minMaxDefrost called ")
    if (parseFloat(value) < min){
        return min;
        }
    else if (parseFloat(value) > max){
        return max;
        }
    else {
        return Math.floor(value);
    }
}


minMaxDefrost(4, 0, 12);

</script>
</html>
  • I use this function in a html keyup function and it works fine but when i try to call it by itself at the start of the code i get the error message Uncaught TypeError: value.substring is not a function我在 html keyup 函数中使用此函数,它工作正常,但是当我尝试在代码开头自行调用它时,我收到错误消息 Uncaught TypeError: value.substring is not a function
  • also not when i call this function from the keyup i use "this.value = minMaxDefrost(this.value, -80, 150, 0)也不是当我从 keyup 调用这个函数时,我使用“this.value = minMaxDefrost(this.value, -80, 150, 0)
  • im assuming the error has got to do with the this.value but i dont know how to fix it我假设错误与 this.value 有关,但我不知道如何修复它

isNumber(4) evaluates to false , so you're trying to call a substring method on 4 but that doesn't exist. isNumber(4)计算结果为false ,因此您尝试在4上调用substring方法,但该方法不存在。 Use typeof value === 'number' instead to test if something is a number.使用typeof value === 'number'来测试某个东西是否是数字。 Or better yet, use typeof value === 'string' before treating it like it's definitely a string.或者更好的是,在将它视为绝对是字符串之前使用typeof value === 'string'

isNumber doesn't exactly do its job as Mark Hanna suggests.. a better isNumber would be something like isNumber并没有像 Mark Hanna 建议的那样完全完成它的工作......更好的isNumber应该是这样的

function isNumber(num){
  let num1=num-0, num2=num-1
  return num2!=num1 && !!(num2||num1) && typeof(num)!="object"
  //Infinity would fail the test num2!=num1
  //Normal non-numbers would fail !!(num2||num1)
  //null would fail typeof(null)!="object"
  //However, 0, and even "0" will pass this test
}

Here is it returned in the code you gave us这是您提供给我们的代码中返回的

 function isNumber(num){ let num1=num-0, num2=num-1 return num2!=num1 && !!(num2||num1) && typeof(num)!="object" //Infinity would fail the test num2!=num1 //Normal non-numbers would fail !!(num2||num1) //null would fail typeof(null)!="object" //However, 0, and even "0" will pass this test } function minMaxDefrost(value, min, max) { if (value.length == 0 || value == "-") return value; if (!isNumber(value)) return value.substring(0, value.length - 1); console.log("minMaxDefrost called ") if (parseFloat(value) < min){ return min; } else if (parseFloat(value) > max){ return max; } else { return Math.floor(value); } } minMaxDefrost(4, 0, 12);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能帮我告诉我为什么我在函数中得到一个空值 - Can anyone help and tell me why do i get a null value in the function 当我将 animation 效果包装在 function 中时,它停止工作。 谁能告诉我为什么? - When I wrap my animation effect in a function, it stops working. Can anyone tell me why? 谁能告诉我为什么我在控制台中收到 404 错误? - Can anyone tell me why I'm getting a 404 error in the console? 谁能告诉我为什么这个javascript函数调用不起作用? - Can anyone tell me why this javascript function call isn't working? 谁能告诉我为什么我收到错误'无法读取未定义的属性'值''? P5.JS - Can anyone tell me why i am getting the error 'Cannot read property 'value' of undefined'? P5.JS 当我调用函数时,它给了我错误。 为什么? - When i call function it's given me error. why? 谁能告诉我为什么会发生MySQL语法错误? - Can anyone tell me why this occur MySQL syntax error? 谁能让我知道为什么会收到此错误? - Can anyone let me know why am I getting this error? 任何人都可以告诉我为什么我无法删除待办事项列表中的项目? - can anyone tell me why i am unable to delete an item from the todo list? 谁能告诉我为什么我收到Uncaught SyntaxError:意外的字符串? - Can anyone tell me why I am getting Uncaught SyntaxError: Unexpected string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM