What is a lambda (function)?

后端 未结 23 2270
太阳男子
太阳男子 2020-11-22 04:47

For a person without a comp-sci background, what is a lambda in the world of Computer Science?

23条回答
  •  渐次进展
    2020-11-22 05:02

    In Javascript, for example, functions are treated as the same mixed type as everything else (int, string, float, bool). As such, you can create functions on the fly, assign them to things, and call them back later. It's useful but, not something you want to over use or you'll confuse everyone who has to maintain your code after you...

    This is some code I was playing with to see how deep this rabbit hole goes:

    var x = new Object;
    x.thingy = new Array();
    x.thingy[0] = function(){ return function(){ return function(){ alert('index 0 pressed'); }; }; }
    x.thingy[1] = function(){ return function(){ return function(){ alert('index 1 pressed'); }; }; }
    x.thingy[2] = function(){ return function(){ return function(){ alert('index 2 pressed'); }; }; }
    
    for(var i=0 ;i<3; i++)
        x.thingy[i]()()();
    

提交回复
热议问题