Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should read this question:</p> <p><a href="https://stackoverflow.com/questions/145053/javascript-as-a-functional-language">Javascript as a functional language</a></p> <p>There are lots of useful links, including:</p> <ul> <li><a href="http://www.ibm.com/developerworks/library/wa-javascript.html" rel="nofollow noreferrer">Use functional programming techniques to write elegant JavaScript</a></li> <li><a href="http://javascript.crockford.com/little.html" rel="nofollow noreferrer">The Little JavaScripter</a></li> <li><a href="http://interglacial.com/hoj/hoj.html" rel="nofollow noreferrer">Higher-Order JavaScript</a></li> <li><a href="http://eloquentjavascript.net/chapter6.html" rel="nofollow noreferrer">Eloquent JavaScript, Chapter 6: Functional Programming</a></li> </ul> <p>Now, for my opinion. A lot of people <a href="http://javascript.crockford.com/javascript.html" rel="nofollow noreferrer">misunderstand JavaScript</a>, possibly because its syntax looks like most other programming languages (where Lisp/Haskell/OCaml look completely different). JavaScript is <em>not</em> object-oriented, it is actually a <a href="http://en.wikipedia.org/wiki/Prototype-based_programming" rel="nofollow noreferrer">prototype-based language</a>. It doesn't have classes or classical inheritance so shouldn't really be compared to Java or C++.</p> <p>JavaScript can be better compared to a Lisp; it has closures and first-class functions. Using them you can create other functional programming techniques, such as <a href="http://ejohn.org/blog/partial-functions-in-javascript/" rel="nofollow noreferrer">partial application</a> (currying).</p> <p>Let's take an example (using <code>sys.puts</code> from node.js):</p> <pre><code>var external; function foo() { external = Math.random() * 1000; } foo(); sys.puts(external); </code></pre> <p>To get rid of global side effects, we can wrap it in a closure:</p> <pre><code>(function() { var external; function foo() { external = Math.random() * 1000; } foo(); sys.puts(external); })(); </code></pre> <p>Notice that we can't actually do anything with <code>external</code> or <code>foo</code> outside of the scope. They're completely wrapped up in their own closure, untouchable.</p> <p>Now, to get rid of the <code>external</code> side-effect:</p> <pre><code>(function() { function foo() { return Math.random() * 1000; } sys.puts(foo()); })(); </code></pre> <p>In the end, the example is not purely-functional because it <strong>can't</strong> be. Using a random number reads from the global state (to get a seed) and printing to the console is a side-effect.</p> <p>I also want to point out that mixing functional programming with objects is perfectly fine. Take this for example:</p> <pre><code>var Square = function(x, y, w, h) { this.x = x; this.y = y; this.w = w; this.h = h; }; function getArea(square) { return square.w * square.h; } function sum(values) { var total = 0; values.forEach(function(value) { total += value; }); return total; } sys.puts(sum([new Square(0, 0, 10, 10), new Square(5, 2, 30, 50), new Square(100, 40, 20, 19)].map(function(square) { return getArea(square); }))); </code></pre> <p>As you can see, using objects in a functional language can be just fine. Some Lisps even have things called property lists which can be thought of as objects.</p> <p>The real trick to using objects in a functional style is to make sure that you don't rely on their side effects but instead treat them as immutable. An easy way is whenever you want to change a property, just create a <em>new</em> object with the new details and pass that one along, instead (this is the approach often used in Clojure and Haskell).</p> <p>I strongly believe that functional aspects can be very useful in JavaScript but ultimately, you should use whatever makes the code more readable and what works for you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload