Harmony, JavaScript for Web apps

New features of JavaScript and tests of implementation in browsers.

This page also includes JavaScript additions earlier to Harmony, that Firefox has implemented, but not other browsers.

For generators, grouped assigment, let to work, it is necessary to specify the version of JavaScript:

<script type="application/javascript;version=1.7">
Itérator (1.7)

Object to process in sequence the elements it contains.


var lang = { 0 : 'JavaScript', 1: 'php' };
var it = Iterator(lang);
var n = it.next();

Generator (1.7)

This provides a form of concurrency to JavaScript. Every time you call the function, you go to the next yield and get the associated value, even if yield is in a loop.
In the example below it displays successively one, two, three, 1, 2 and false because i must be less than 3.

Generator and reserved word yield:

function simpleGenerator(){
  yield "one";
  yield "two";
  yield "three";
  for (var i = 0; i < 3; i++)
    yield i;
var g = simpleGenerator();
document.write(g.next());  // displays 'one'
Scoping (1.7)

Let is an alternative to var for restricting visibility to the body of a block, rather than to a function.

Let :

var testlet = 10;
if(true) {
  let testlet = 20;
  if(testlet == 20)
alert(testlet); // displays 10.
Destructuring assignement (1.7)

This allows you to make grouped assignments. Including from the return of a function.

[a , b ] = [x , y];

var ada = 1;
var adb = 2;
var adx = 3;
var ady = 4;
[ada , adb] =  [adx, ady];  // ada now is 3 and adb 4

Set (Harmony)

This new type of array can not be confused with an associative array. The numeric keys are always keys, not indices.
In the example, 1 is part of the list.

Set/add()/has()/size :

var testset = new Set();
document.write(testset.has(1));  // return true.

Map (Harmony)

This new type of associative array (name with a capital letter unlike the method) has a size attribute unlike conventional arrays which are actually dynamic objects. The set and get methods modify its content.

Map/set()/get()/size :

var testmap = new Map();
testmap.set("a", "one");
testmap.set("b", "two");
document.write(testmap.get("b")); // displays two.

String.trim() (1.8.1)

New String method that removes extra front and trailing spaces . Support IE 9.

String.trim() :

var tests = " Hello ";
document.write(tests.trim()); // displays "Hello".

Class (Harmony)

True classes with inheritance.

Class :

var tclass = class {
  constructor() { }
  tmet() {
    return "ok"; 
document.write(tclass.tmet()); // should display ok.

Promise (Harmony)

An object to wait for the end and the result of a preliminary process before to continue a processing, asynchronously.

Promise :

var tprom = new Promise(function(a, b) {

function success(res) { 

tprom.then(promtrue, function(res) {});

See also...

© 2013-2015 Xul.fr