Category talk:JavaScript: Difference between revisions

From Rosetta Code
Content added Content deleted
(it seems it is)
(Explained why JavaScript isn't functional)
Line 1: Line 1:
Javascript is a functional language? --[[User:Short Circuit|Michael Mol]] 10:45, 29 May 2010 (UTC)
Javascript is a functional language? --[[User:Short Circuit|Michael Mol]] 10:45, 29 May 2010 (UTC)
: It seems it is (too), according to several sources. --[[User:ShinTakezou|ShinTakezou]] 17:42, 29 May 2010 (UTC)
: It seems it is (too), according to several sources. --[[User:ShinTakezou|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? Absolutely nothing. You can't create an immutable object, you can't put specify the type of your variables, you can't enforce functional purity, and you can't even put things into namespaces (everything is global). There is also implied global variables.

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 PHP 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 admittedly, the Mozilla-specific JavaScript 1.8 does 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 in JavaScript. As such, most of the time programmers use prototypal programming and object systems like Joose and JS.Class for code re-use purposes. 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 hygienic macros, homoiconicity, and an effects system (side-effect causing functions have a ! at the end of them in Scheme). These things together make JavaScript vastly different from Scheme. --[[User:Jhuni|Jhuni]] 31 December 2010, 14:14 HST

Revision as of 00:15, 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? Absolutely nothing. You can't create an immutable object, you can't put specify the type of your variables, you can't enforce functional purity, and you can't even put things into namespaces (everything is global). There is also implied global variables.

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 PHP 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 admittedly, the Mozilla-specific JavaScript 1.8 does 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 in JavaScript. As such, most of the time programmers use prototypal programming and object systems like Joose and JS.Class for code re-use purposes. 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 hygienic macros, homoiconicity, and an effects system (side-effect causing functions have a ! at the end of them in Scheme). These things together make JavaScript vastly different from Scheme. --Jhuni 31 December 2010, 14:14 HST