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...)
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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.
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.
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.
Repeating my own answer to a similar question,
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.
@petraszd I rewrite your code a little to obtain a "new" for operator:
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