Note that there are some explanatory texts on larger screens.

plurals
  1. POHow is a functional programming-based JavaScript app laid out?
    primarykey
    data
    text
    <p>I've been working with <a href="http://nodejs.org/" rel="noreferrer">node.js</a> for a while on a chat app (I know, very original, but I figured it'd be a good learning project). <a href="http://documentcloud.github.com/underscore/" rel="noreferrer">Underscore.js</a> provides a lot of functional programming concepts which look interesting, so I'd like to understand how a functional program in JavaScript would be setup. </p> <p>From my understanding of functional programming (which may be wrong), the whole idea is to avoid side effects, which are basically having a function which updates another variable outside of the function so something like</p> <pre><code>var external; function foo() { external = 'bar'; } foo(); </code></pre> <p>would be creating a side effect, correct? So as a general rule, you want to avoid disturbing variables in the global scope. </p> <p>Ok, so how does that work when you're dealing with objects and what not? For example, a lot of times, I'll have a constructor and an init method that initializes the object, like so:</p> <pre><code>var Foo = function(initVars) { this.init(initVars); } Foo.prototype.init = function(initVars) { this.bar1 = initVars['bar1']; this.bar2 = initVars['bar2']; //.... } var myFoo = new Foo({'bar1': '1', 'bar2': '2'}); </code></pre> <p>So my init method is intentionally causing side effects, but what would be a functional way to handle the same sort of situation?</p> <p>Also, if anyone could point me to either a Python or JavaScript source code of a program that tries to be as functional as possible, that would also be much appreciated. I feel like I'm close to "getting it", but I'm just not quite there. Mainly I'm interested in how functional programming works with traditional OOP classes concept (or does away with it for something different if that's the case).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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