- Is Javascript a functional language? I know it has objects & you can do OOP with it also, but is it also a functional language, can it be used in that way?
- You know how OOP became/seems like the next evolution in programming, does that mean that 'Functional Programming' is the next evolution(Note: this is NOT a prompt for opinion BUT a prompt for a factual evidence based answer, & this note is more for the moderators than the contributors ;) ).
- I learn best through examples, maybe someone could show performing the same task in a OOP way & then in a Functional Programming way for myself to understand & compare what functional programming does/is.
I don't really completely understand 'Functional Programming' to be honest :P So comparing Javascript to functional programming may be totally incorrect.
To put Functional programming in laymans terms: is it simply the benefit of abstration THROUGH using anonymous functions?
Or is that way too simple? In a simple way, OOP is the benefit of abstraction through objects, but I believe thats being a little too simplistic to describe OOP.
Is this a good example of functional programming?...
Javascript OOP Example:
// sum some numbers
function Number( v )
{
this.val = v;
}
Number.prototype.add( /*Number*/ n2 )
{
this.val += n2.val;
}
Functional programming example:
function forEach(array, action)
{
for (var i = 0; i < array.length; i++)
action(array[i]);
}
function add(array)
{
var i=0;
forEach(array, function(n)
{
i += n;
});
return i;
}
var res = add([1,9]);