views:

147

answers:

2

I've recently been looking at functional programming with Javascript, to which I'm a noob.

While writing some 'map', 'reduce' and 'find' functions I discover that as of JS version 1.5 these functions are already available (see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array)

I am however confused as next to these functions (i.e reduce) it says 'requires javscript 1.8' - but its in the 1.5 docs? How can this be ?

Also does anyone have a list, of all the major browsers, against which version of javascript they are running ?

If I wanted to use functional programming in my web project which approach should I be using ? Should I include a library of functions or can I rely on the browsers implementations ?

+6  A: 

You won't be able to rely on the built-in implementations of these methods unless you know your userbase is 100% firefox 3.

However, you can code your implementations with the idea that they might already exist, as seen in the docs

if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }

    return res;
  };
}
Peter Bailey
When will this be released as the standard (I mean for all browsers)?
the_drow
@the_drow: Whenever other browser vendors decided to implement it.
musicfreak
+3  A: 

http://en.wikipedia.org/wiki/JavaScript#Versions

I think the biggest issue is that these changes are unofficial, and the ecmascript standard (on which javascript is based) has been bogged down in discussions for years.

The good news is that ECMAScript 5 is finally on a good path, they'll probably have a final spec by the end of the year, and all browser vendors will likely commit to implementing it during 2010. So, by the end of 2010 we should get map/reduce on the array object.

You can read the draft spec here:

http://www.ecma-international.org/publications/files/drafts/tc39-2009-025.pdf

(3 meg PDF file)

Joeri Sebrechts