Spread element magically turns functions into 'not functions'

前端 未结 4 913
北恋
北恋 2021-01-07 23:37

Suppose I have this simple JavaScript function:

function returnArray(){
    return [1, 2, 3];
}

Further suppose that I then say

<         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-08 00:16

    Here is the error I get using node@6.10.3

    if (rest.length!=0) return [2*num, ...double(rest)];  
                                            ^
    
    TypeError: double(...)[Symbol.iterator] is not a function
        at double (/home/henrique/labs/test.js:4:41)
        at double (/home/henrique/labs/test.js:4:41)
        at Object. (/home/henrique/labs/test.js:7:1)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:390:7)
    

    Which probably means that the result of the evaluation of some of your function call is not an iterable.

    The error is here:

    if (array.length===1) return 2*array[0];
    

    Change to:

    if (array.length===1) return [2*array[0]];
    

    and it will work.

提交回复
热议问题