About javascript function closure
Dear all:
I try to improve the ability of javascript, and meet a problem about closure, like following example:
- function Y(f) {
- return ((function(x) {
- return f(function(v) {
- return x(x)(v);
- });
- })(function(x) {
- return f(function(v) {
- return x(x)(v);
- });
- }));
- }
- var factorial = Y(function(fac) {
- return function(n) {
- return (n == 0 ? 1 : n * fac(n - 1));
- }
- });
- console.log( factorial(5));
I don't know how the function Y(f) works. I have add console.log to see the detail , but still confuse
and try to fill in the function by myself, but I found I don't know how to create this function and
make it work well. Is any one know the exact answer for that, and how to make that, thanks a lot.