使用导致函数的参数始终返回false

Dr.*_*nni -1 javascript if-statement

当且仅当两个args是数字(因此我的第一个函数)时,我想总结我的函数的args.

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, "");
Run Code Online (Sandbox Code Playgroud)

然而,无论args值是什么,我的第二个函数都会执行求和.关于如何解决这个问题的任何提示?

Sco*_*cus 6

checkNum()没有被声明为显式地接受任何参数(这意味着任何人都在看这个函数时没有预期),并且当你调用它时你没有发送任何参数,所以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");
Run Code Online (Sandbox Code Playgroud)

现在,根据您的"数字"标准,您可以采用两种方式.

  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
Run Code Online (Sandbox Code Playgroud)

  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"
Run Code Online (Sandbox Code Playgroud)

  • @KyleSposato是的,但在这种情况下,`arguments`将为空 (3认同)