Mozilla Extending Javascript?
Nomad128 writes "Mozilla's Deer Park 1 Alpha RC appears to have extended the Javascript spec for the first time in quite some time. New features include Array object methods "every" (logical AND), "some" (logical OR), "map" (function mapping), and "forEach" (iteration). They also appear to have added native XML support. Will this speed up the development of AJAX applications and give Moz a leg-up over IE7?"
If they are useful for people developing on the "mozilla platform", then they are useful features. For example, Firefox and Thunderbird, and extensions to each, can use these new features. They can't be used in web pages unless you want them to be Mozilla-only, of course.
Napster-to-go says "Fill and refill your compatible MP3 player", which is a lie. It's not MP3. It's WMA with DRM.
Well, I only had a quick look, but these extensions don't appear to be very difficult to reproduce in other browsers...
The following mimics the forEach extension - and works in Mozilla, Opera and IE
Array.prototype.forEach = function(fn) {
for(var i =0; i this.length; ++i) {
fn(this[i], i, this);
}
}
function foo(obj, index, array) {
alert("index " + index + " is " + obj);
}
[4,5,6].forEach(foo);
(Only had a quick look at the Mozilla article and 5 mins knocking the source up, so excuse any silly errors)
Ditto for "map". Actually I didn't notice they also specified an optional "this object" for forEach()...
Array.prototype.map = function(fn, array) {
var thisObject = array == undefined ? this : array;
var result = [];
for(var i =0; i thisObject.length; ++i) {
result.push(fn(thisObject[i]));
}
return result;
}
function makeUpperCase(obj) {
return obj.toUpperCase();
}
strings = [ "hello", "Array", "WORLD" ];
uppers = strings.map(makeUpperCase);
alert(uppers);
alert(strings);
ECMA spec allows you to add in anything you want...
A conforming implementation of ECMAScript is permitted to provide additional types, values, objects,
properties, and functions beyond those described in this specification. In particular, a conforming
implementation of ECMAScript is permitted to provide properties not described in this specification, and
values for those properties, for objects that are described in this specification.
This entry in Asa Dotzler's blog contains links for downloading this release candidate of Deer Park Alpha 1.1.
The article has links to New Web Developer Features and New Extension Developer Features. There's also a page listing New Browser Features and an unofficial page listing Notable bug fixes.
The shareholder is always right.