Art Of Programming

musings by Dmytrii Nagirniak

Firefox 3: New in JavaScript 1.8

So what are the new stuff in the JavaSscrip 1.8 implemented in Firefox 3? Let’s go through it quickly:

First of all JS 1.9 is enaable only by adding following script tag:

 <script type="application/javascript;version=1.8"> ... your code ... </script>

Very interesting thing is Lambda notation which is a bit similar to C# Lambda Expressions.

Code is worth to show:

JavaScript 1.7 and older:

 function(x) { return x * x; }

JavaScript 1.8:

 function(x) x * x

It looks very nice, but I’m not sure how readable such code will be if this feature will be overused.

 

Also Generator Expressions look interesting. But the code might not always be obvious to read.

Compare the samples:

In JavaScript 1.7, you might write something like the following in order to create a custom generator for an object:

 function add3(obj) {
   for ( let i in obj )
     yield i + 3;
 }
 
 let it = add3(someObj);
 try {
   while (true) {
     document.write(it.next() + "<br>\n");
   }
 } catch (err if err instanceof StopIteration) {
   document.write("End of record.<br>\n");
 }

In JavaScript 1.8, you can circumvent having to create a custom generator function by using a generator expression instead:

 let it = (i + 3 for (i in someObj));
 try {
   while (true) {
     document.write(it.next() + "<br>\n");
   }
 } catch (err if err instanceof StopIteration) {
   document.write("End of record.<br>\n");
 }

Behind the thing that I have never used let statement, I’m don’t believe it is supported in IE (7) which I must target. However,

(i + 3 for (i in someObj)) looks nice.

 

Array object has two additional methods: reduce and reduceRight. Very useful things that are available (in a similar way) in most JS libraries (ExtJS has ForEach) that iterates through all the elements of an array. Nice to have a build-in support for it, but since it is not a part of ECMA-262 standard I’d never use it.

 

So, as far as I can see the JavaScript 1.8 implementation is intended to allow developers write less code. But I’m not going to use it just now and just because of I’d rather write some additional "old"-manner code lines but have cross-browser support.

Comments