Is Javascript a Functional Programming Language?

前端 未结 13 847
天涯浪人
天涯浪人 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:44

    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.

    0 讨论(0)
  • 2020-12-04 05:44

    What I really hate in javascript (if You try to look at it as FP language) is this:

    function getTenFunctionsBad() {
      var result = [];
      for (var i = 0; i < 10; ++i) {
        result.push(function () {
          return i;
        });
      }
      return result;
    }
    
    function getTenFunctions() {
      var result = [];
      for (var i = 0; i < 10; ++i) {
        result.push((function (i) {
          return function () {
            return i;
          }
        })(i));
      }
      return result;
    }
    
    var functionsBad = getTenFunctionsBad();
    var functions = getTenFunctions()
    for (var i = 0; i < 10; ++i) {
      // using rhino print
      print(functionsBad[i]() + ', ' + functions[i]());
    }
    
    // Output:
    //   10, 0
    //   10, 1
    //   10, 2
    //   10, 3
    //   10, 4
    //   10, 5
    //   10, 6
    //   10, 7
    //   10, 8
    //   10, 9
    

    You need to understand JS stack environment (I don't if it is the right term) to understand such a behavior.

    In scheme for example You just can't produce such thing (Ok, ok -- with the help of underlying languages' references You can make it):

    (define (make-ten-functions)
      (define (iter i)
        (cond ((> i 9) '())
              (else (cons (lambda () i) (iter (+ i 1))))))
      (iter 0))
    
    (for-each (lambda (f)
                (display (f))
                (newline)) (make-ten-functions))
    
    0 讨论(0)
  • 2020-12-04 05:45

    The term "functional programming" language is so overloaded these days it's almost useless. There are two dominant meanings:

    1. Has first-class functions
      • Javascript is this!
    2. Is based on functions as used in the lambda calculus, with an emphasis on avoiding persistent mutable state (often replacing it with parameters passed to functions)
      • As commonly written, Javascript is not remotely this!

    Pick your meaning and then the question is answerable.

    0 讨论(0)
  • 2020-12-04 05:48

    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.

    0 讨论(0)
  • 2020-12-04 05:55

    if you stretch and twist the term "functional programming" to the point of philosophical discussions, this question may be open again. However, then you end up on the level of useful questions like "Is C++ really a programming language"?

    The answer to your question on more daily level is "no".

    Functional programming means that the program is conceptualized as a evaluation of a function, rather than a control flow. The code is a description of functions, and has no inherent concept of a control flow.

    JavaScript has got a control flow and is conceptualized as a imperative language. From its design objective, it is clearly not a functional language.

    0 讨论(0)
  • 2020-12-04 05:57

    Javascript is to a point. It truly depends on how you go about programming it. If I code in an OO manner, would it not be OO? So if you just code things in a 'functional' manner it would be functional. I guess it is multi-paradigm language so to call it just one thing isn't entirely accurate.

    0 讨论(0)
提交回复
热议问题