Recursion & Javascript dontJavaScript don't mix.
I am a big fan of recursion, but with javascriptJavaScript there are some issues that make recursion a dangerous animal to play with.
One of javascriptsJavaScript's limitations, lack of proper tail calls, (ES6 has it slated but there is a world of noncompliance out there) is both a saving grace and a curse.
Your function deals with it and exits. But there are many objects in the javascriptJavaScript environment that are self referencing. It is perfectly normal for me to do,
This is because there is a limit to the depth of the call stack. It is also why recursion is not always safe. It may be ridiculous in this example but a recursive function can not be said to be safe. You can not know when you will exceed the call stack, especially if you are calling the recursive function innocently from within another recursive function. You can not enforce calling conditions so recursion is really something to be avoided in javascriptJavaScript.
So the rest will ignore the fact that you should not be writing recursive code in javascriptJavaScript unless you extremely careful.
- Bad naming. Functions
eachandisLeaftofindPrimitivesandisPrimitive - It is not immediately clear what the callback function is for, and its return type is a ambiguous. seeSee below for more.
- Memory usage. Don't use Array.concat inside recursive code, if you can avoid it.
Array.concatinside recursive code, if you can avoid it.Array.concatcreates a new referancereference to the array, so each step into the recursion you create a new referancereference that is closed over, effectively creating may similar copies of the same data. isleafnowisPrimitiveis failing. There is a more robust way to test if the current "leaf" is a primitive type. See rewrite atrat bottom.
The callback function is ambiguous. One of javascriptsJavaScript's flaws is no way to force a return type for a function. Many people consider undefined == false, and not undefined !== false and would expect that the unspecified return value (functions return undefined by default) to mean false. I would remove the ambiguity and require that the callback function answer the question "Has the object you are looking for been found?" and return true to exit from the recursion"
- There are some differing schools of thought as to the use of the
lettoken and the importance of block scope. When I see it being used inconsistently and interchangeably with eitherconstor (in this case)var, I consider that it is not being correctly used and must consider if in fact the author has sound logic for creating block scoped variables or just using a trendy new feature because it is there. - There were many wayways to make the function fail, though it is only an exercise, testing is still very important. Bad habits can form and incorrect assumption made when you don't test your experiments to their destruction.
- I am not a fan of anonymous functions if they can be avoided as it make debugging so much more difficult. I have seen traces that are just anon calling anon all the way from the top,the only way to trace the actual path is to step out one by one.
- For all of us JavaScript programmers it is a time of transition. Some will use ES6 to the full, and others avoid it because of the legacy browsers (Dam you IE for freezing just as ES6 was adopted!). But with ES6 it's in for a penny, in for a pound. You have clearly opted in for ES6 so you should use it to the full. Currently your code is a mixture of ES5 and ES6, being incompatible to ES5 only environments and not getting the full benefit of ES6.
There are two issues. Your use of let and naming as noted above. andAnd the following
This was hard to do as iI would implement this type of function completely differently. So it is a compromise between your implied requirements (from your code) and my A.D.D. driving me to start it from scratch.
I changed the arguments. Separating out the current object / property name. I keep the same path array referancereference rather than create a new one each time. I use Object.keys to get an array of property names rather than for(k in obj) I vet for callback function. I vet for obj being a primitive or not having properties. I changed isPrimitive to a more suitable solution.