Is Javascript a Functional Programming Language?

2019-01-12 14:52发布

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 main thing I think it lacks is Pure Functions, and it doesn't 'feel' like other functional languages, like lisp (although thats not really a good reason for it not to be a functional langauge...)

13条回答
▲ chillily
2楼-- · 2019-01-12 15:31

I don't think there a concrete definition of functional programming , however many of things people consider "functional programming" can be done with javascript. Here is a good brief example in this article.

查看更多
别忘想泡老子
3楼-- · 2019-01-12 15:32

To me, Javascript is both an imperative language and a functional language, and you can choose to use it either way, and even (egad) both ways. Or you can choose to use one paradigm and never touch the other. It's up to you. I, like you, don't think Javascript should be called a Functional Language, because it allows you to wander in and out of the functional programming paradigm. Perhaps if it had a pragma of some kind, to limit you using only functional programming paradigms, then that would be useful, I think. But, in summary, I say it's more of a imperative/procedural language with some functional programming features tossed in.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-12 15:36

As we know the functional programming language doesn't allow to change or mutate the elements(state)of functions but in javascript it is allowed in that sense it is not a functional programming language, although it does treat function as first class citizens.

查看更多
再贱就再见
5楼-- · 2019-01-12 15:39

Repeating my own answer to a similar question,

There's no accepted definition of functional programming language.

If you define functional language as the language that supports first class functions and lambdas, then yes, JavaScript *is* a functional language.

If you also consider the factors like support for immutability, algebraic data types, pattern matching, partial application etc then no, JavaScript *is not* a functional language.


I'd encourage you to read the following related blog posts (and also the comments below them):

查看更多
太酷不给撩
6楼-- · 2019-01-12 15:40

Well, I wouldn't say it's functional programming, but then I would say it's object oriented and just today a friend said he wouldn't put it on that shelf either.

So, while I wouldn't say it is, I guess there's room for opinion. It does have classical features of functional programming, it doesn't have others.

查看更多
迷人小祖宗
7楼-- · 2019-01-12 15:41

@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

查看更多
登录 后发表回答