Is Javascript a Functional Programming Language?

前端 未结 13 875
天涯浪人
天涯浪人 2020-12-04 05:22

Just because functions are first class objects, there are closures, and higher order functions, does Javascript deserve to be called a Functional Programming language? The

13条回答
  •  误落风尘
    2020-12-04 05:42

    @petraszd I rewrite your code a little to obtain a "new" for operator:

       
       function ffor(a, b, f){
         function it(i){
           if(i > b)return
           f(i)
           it(i+1)
         }
         it(a)
       }
    
       print("----" + new Date()+"----")
    
       var funcs = []
       ffor(0, 9, function(i){
         funcs.push(function(){return i})
       })
    
       ffor(0, 9, function(i){
         print(funcs[i]())
       })
    

    But I know that this way has disadvantages for big loops...

    Related question about tail recurtion optimization in JS

    P.S. Posted here cuz have problem with code formatting while posting as comment

提交回复
热议问题