aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes JavaScript functional programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JavaScript functional programming" Watch "JavaScript functional programming" New topic
Author

JavaScript functional programming

Tomasz Prus
Ranch Hand

Joined: May 20, 2008
Posts: 73
Is it possible to use Javascript in functional (applicative) programming style? Does it have a facilities fo FP? Propably i'm wrong but some guy said that i should embrace JS as functional language.
[ August 27, 2008: Message edited by: Ulf Dittmer ]
Stoyan Stefanov
author
Ranch Hand

Joined: Jul 16, 2008
Posts: 61
Short answer: yep

Functions in JavaScript are first-class objects. You have function literals, you can store functions in variables, add/remove their properties and so on. This gives you a great expressive power.



An interesting pattern for code reuse that comes to mind is stealing (or borrowing) methods.

Imagine this object:


Now another object:


Now 'hoho' doesn't have a getName() method but it can borrow it from 'o':



This way getName() is invoked and 'hoho' is bound to 'this' inside getName()


<a href="http://www.thinkinginjavascript.com" target="_blank" rel="nofollow">my OOJS book</a>
 
Consider Paul's rocket mass heater.
 
subject: JavaScript functional programming