Category talk:JavaScript: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 6: Line 6:
Functional programming languages try to reduce side effects to a minimum, this usually involves immutable data structures and pure functions and then dropping down to something like Monads when side effects are needed.
Functional programming languages try to reduce side effects to a minimum, this usually involves immutable data structures and pure functions and then dropping down to something like Monads when side effects are needed.


What about JavaScript? What does JavaScript do to reduce or limit side effects in your program? Absolutely nothing.
What about JavaScript? What does JavaScript do to reduce or limit side effects in your program?


: No way of enforcing functional purity
: No way of enforcing functional purity

Revision as of 00:26, 1 January 2011

Javascript is a functional language? --Michael Mol 10:45, 29 May 2010 (UTC)

It seems it is (too), according to several sources. --ShinTakezou 17:42, 29 May 2010 (UTC)

Effects System:

Functional programming languages try to reduce side effects to a minimum, this usually involves immutable data structures and pure functions and then dropping down to something like Monads when side effects are needed.

What about JavaScript? What does JavaScript do to reduce or limit side effects in your program?

No way of enforcing functional purity
No way of creating immutable objects
You can't statically type variables
You can't even put things into namespaces

JavaScript has the weakest control over its side effects of any language I know of, there is even implied globals if you leave off the var keyword.

First Class Functions

JavaScript has first class functions. But so does Lua, Perl, Ruby, Python as of 1994, PHP 5.3, Visual Basic 9, C# 3.0, and even C++0x. If JavaScript's support for first class functions is all it takes to make it functional you should also add Visual Basic and all of those other languages which have that too. A much more interesting category would be all of those languages which don't have first class functions, C, Java, and a few other languages don't have them.

What about parameter handling features? In C++ you can declare a parameter to be const to enforce const-correctness. In JavaScript you can't modify your parameters at all, they always come in as mutable variables. There is also no parameter types or default parameters.

Additionally, most functional programming languages have implicit return so that you don't have to write out a return statement. Although the Mozilla-specific extension JavaScript 1.8 does have expression closures with implicit return.

How is JavaScript used?

JavaScript puts everything into a single mutable global namespace. There is no way to export functions into your module, or other features which would make functional programming a sane choice .. As such, most of the time programmers use prototypal programming and object systems like Joose and JS.Class for code re-use. As such JavaScript is mostly a prototypal programming language, or an OOP language, and not a functional programming language.

As for Scheme, I do not think it is comparable to JavaScript. It has homoiconicity, hygienic macros, and a decent effects system (side-effect causing functions are suffixed with !). These things together make it vastly different from JavaScript. --Jhuni 0:15, 1 January 2011, (UCT)