Recursive function call wrapped in anonymous function not found

Note. This is mainly theoretical practice.

function one() { return [1, function() { one(); }]; } console.log((one()[1])()); 

The output gives undefined . Why?

+4
source share
1 answer

Split it up:

 function one() { return [1, function() { one(); }]; } console.log((one()[1])()); one(); // [1, function() { one(); }] [1] // function() { one(); } () // undefined 

If you return one() , it will return an array:

 function one() { return [1, function() { return one(); }]; } console.log((one()[1])()); one(); // [1, function() { return one(); }] [1] // function() { return one(); } () // [1, function() { return one(); }] 
+7
source

Source: https://habr.com/ru/post/1483905/


All Articles