2017-06-26 32 views
-1

我想總結我的函數的args當且僅當兩個參數是數字(因此我的第一個函數)。使用參數導致函數總是返回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, ""); 

但是我的第二個函數執行的總和不管什麼參數值。有關如何解決這個問題的任何提示?

+0

永遠不要使用這樣的'arguments'對象。顯式聲明你的參數,並*將它們明確地作爲參數傳遞,所以你很快就會注意到這裏沒有工作。 – Bergi

回答

5

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");

現在,根據你的標準的 「數字」,有兩種方法,你可以走了。

  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

  • 數字數字數字個字符被允許(即, 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"

    +1

    'checkNum()'不需要顯式聲明參數。 'arguments'是獲取傳遞給JavaScript函數參數的有效方法。 [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) – KyleS

    +3

    @KyleSposato是的,但'arguments'在這種情況下將是空的 –

    +1

    @KyleSposato我didn不要說'arguments'不是訪問參數的有效方法。我解釋說,在OP的情況下,沒有人傳遞給'checkNum()',因此'arguments.length'將始終爲0.我認爲這很清楚。 –

    1

    arguments數組,內checkNum評價,包含傳遞到checkNum的參數。但是你沒有將任何論點傳遞給checkNum。嘗試將if語句更改爲

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

    非常感謝。這解決了這個問題。 –

    -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, ""); 
    
    +0

    downvote背後的任何解釋? 「應用」不鼓勵?我從來沒有遇到過問題。 –