繁体   English   中英

使用参数使函数始终返回false

[英]Use of arguments causing function to always return false

我想在且仅当两个参数是数字时才对函数的参数求和(因此是我的第一个函数)。

function checkNum() {
 var num = 0;
 for (var i = 0; i < arguments.length; i++) {
  if (typeof arguments[i] !== 'number') {
   return false;
  } 
 }
 return true;
}

function addTogether() {
  var num = 100; 
  if ( checkNum() ) { 
    return arguments[0] + arguments[1];
  } else {
    return undefined;
 }
}
addTogether(2, "");

但是,无论args值是多少,我的第二个函数都会执行求和。 关于如何解决此问题的任何提示?

没有声明checkNum()显式接受任何参数(这意味着任何查看该函数的人都不会期望),并且在调用它时不发送任何arguments.length ,因此arguments.length始终为0,因此您永远不会输入循环主体,您总是返回true

您的第二个函数通过传递两个参数来调用,因此您对arguments[0]arguments[1]引用在那里有效。 但是,即使如此,使用arguments并不是真正意义上的所有参数传递。

最好使用命名参数设置函数,然后可以通过这些名称访问它们。 不鼓励使用arguments (有效时)作为访问参数的默认机制。 它通常用于验证(例如,在函数尝试对其进行操作之前,请确保已将正确数量的参数传递给该函数)。

另外,最好使用正则表达式测试数字,因为typeof可能对您“说谎”。 例如:

  // Would you ever think that not a number is of type "number"?! console.log(typeof NaN === "number"); 

现在,根据您的“数字”标准,您可以采用两种方法。

  1. 只包含数字被允许(即6被允许,“6”是不是)

 // It's better for this function to test one number // at a time, so you can react to that particular // success or failure function checkNum(num) { // No loop and no if/then needed, just return // whether the argument is a number, but don't // test for typeof number because typeof NaN === "number" // Use a regular expression instead var reg = /[0-9]+$/; // digits or strings of characters that are from 0 - 9 // Test for only digits not numbers passed as strings // For example 6 is good, "6" is bad. Here, the use of "typeof" // is safe because you are also testing that the input is digits // or characters from 0 to 9 (NaN wouldn't pass this test) return reg.test(num) && typeof num === "number"; // true or false will be returned } function addTogether(val1, val2) { // Test each input, independantly so that you can react more granularly if ( checkNum(val1) && checkNum(val2) ) { return val1 + val2; } // It's not necessary to have an "else" that returns undefined because // that's what will happen as long as you don't return anything else. } console.log(addTogether(2, "")); // undefined console.log(addTogether(2, 6)); // 8 console.log(addTogether(2, "6")); // undefined because "6" is a string, not a digit 

  1. 允许使用数字数字字符 (即允许使用6和“ 6”)。 在这种情况下,您需要确保在完成加法运算之前将数字字符转换为数字,以便获得数学加法而不是字符串连接。

 // It's better for this function to test one number // at a time, so you can react to that particular // success or failure function checkNum(num) { // No loop and no if/then needed, just return // whether the argument is a number, but don't // test for typeof number because typeof NaN === "number" // Use a regular expression instead var reg = /[0-9]+$/; // digits or strings that are from 0 - 9 // Test for only digits and numbers passed as strings return reg.test(num); // true or false will be returned } function addTogether(val1, val2) { if ( checkNum(val1) && checkNum(val2) ) { // If checkNum returns true for numeric characters as well as digits, then // you'd need to ensure that the characters get converted to numbers so that // you get mathmatical addition and not string concatenation. That would be done like this: return +val1 + +val2 } // It's not necessary to have an "else" that returns undefined because // that's what will happen as long as you don't return anything else. } console.log(addTogether(2, "")); // undefined console.log(addTogether(2, 6)); // 8 console.log(addTogether(2, "6")); // 8 because "6" is converted to 6, not a string of "6" 

checkNum评估的arguments数组包含传递给checkNum的参数。 但是您没有将任何参数传递给checkNum 尝试将if语句更改为

if ( checkNum(arguments[0], arguments[1]) )

您没有将任何参数传递给checkNum 您可以使用apply解决此问题:

// ...
if (checkNum.apply(this, arguments)) {
// ...

编辑 :这将允许您检查传递给addTogether任意数量的参数。 如果只想允许两个参数,则可以使用命名参数:

function checkNum(a, b) {

    return typeof a === 'number' && typeof b === 'number';
}

function addTogether(a, b) {

    if (checkNum(a, b)) return a + b;
    else return undefined; // line not needed
}

addTogether(2, "");

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM