• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript functional programming

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 85
5
PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
reply
    Bookmark Topic Watch Topic
  • New Topic