本帖最后由 老刘1号 于 2023-3-5 22:29 编辑
多层套娃似神仙- function s(){
- var div_count=0;
- return (function() {;
- div_count ++;
- var obj = {'cnt' : div_count};
- return obj;
- });
- }
- cnt1 = s();
- cnt2 = s();
- console.log(cnt1());
- console.log(cnt1());
- console.log(cnt1());
- console.log(cnt2());
复制代码 当然更离谱的套娃也有,比如匿名函数实现递归的Y组合子- var fn = f => n => n ? f(n-1)*n : 1;
- var Y = y => (x => y(n => x(x)(n)))(x => y(n => x(x)(n)));
- alert(Y(fn)(4));
复制代码
|