Below I have tested the new features in JavaScript 1.7. The code below is also run immediately as you load the page, so you will see the actual results as well. Below each test is the web browsers it works in, and the minimum version of it required.
More information about JavaScript 1.7
function double5() {
var i=0, j=5, k;
while (true) {
yield i;
k = i;
i = j;
j += i;
}
}
var g = double5(),
resultValues = [];
for (var i=0; i<5; i++) {
resultValues.push(g.next());
}
Result: FAILED
2.0+
var lostInfo = {
name : "Lost",
location : "Island",
numbers : "4 8 15 16 23 42"
};
var iteratorObj = Iterator(lostInfo),
resultValues = [],
next;
try{
while (true) {
next = iteratorObj.next();
resultValues.push(next[0] + ": " + next[1]);
}
}
catch (err if err instanceof StopIteration) {
resultValues.push("END OF LIST");
}
catch (err) {
resultValues.push("UNKNOWN ERROR");
}
Result: FAILED
var lostInfo = {
name : "Lost",
location : "Island",
numbers : "4 8 15 16 23 42"
};
// Notice true below - will return only parameter names and not values, as opposed to the example above
var iteratorObj = Iterator(lostInfo, true),
resultValues = [];
try{
while (true) {
resultValues.push(iteratorObj.next());
}
}
catch (err if err instanceof StopIteration) {
resultValues.push("END OF LIST");
}
catch (err) {
resultValues.push("UNKNOWN ERROR");
}
Result: FAILED
2.0+
function setVal (start, end) {
for (let i=start; i<end; ++i) {
yield i;
}
}
var evenValues = [i for each (i in setVal(0, 15)) if (i % 2 == 0)];
Result: FAILED
2.0+
var x = 5,
y = 7,
resultValues = ["Before let statement: " + x + ", " + y];
let (x=15, y=12) {
resultValues.push("In let statement: " + x + ", " + y);
}
resultValues.push("After let statement: " + x + ", " + y);
Result: FAILED
2.0+
var x = 1,
y = 3,
resultValues = ["Before let expression: " + x + ", " + y];
let (x=7, y=8) resultValues.push("In let expression: " + x + ", " + y);
resultValues.push("After let expression: " + x + ", " + y);
Result: FAILED
2.0+
var letDefinitionResults =
document.getElementById("results-let-definition"),
item;
for (var i=0; i<5; i++) {
item = document.createElement("div");
item.innerHTML = "Click me - I will say " + i;
let j = i;
item.onclick = function () {
alert("I am " + j);
};
letDefinitionResults.appendChild(item);
}
Result: FAILED
2.0+
var a = 1, b = 2; // Value swapping [a, b] = [b, a];
Result: FAILED
var a = 1, b = 2, c = 3, d = 4; // Value swapping [a, b, c, d] = [d, c, b, a];
Result: FAILED
var a = 1,
b = 2;
function newValues () {
return [10, 20];
}
// Assigning new values
[a, b] = newValues();
Result: FAILED
var a = 1,
b = 2;
function newValues () {
return [10, 20, 30];
}
// Assigning new values, ignoring the second one
[a, , b] = newValues();
Result: FAILED
2.0+
9.5+ (partial support, some combinations don't work)
var griffins = {
father : "Peter",
mother : "Lois",
son : "Chris",
daugher : "Meg",
baby : "Stewie",
dog : "Brian"
},
resultValues = [];
for (let [type, name] in Iterator(griffins)) {
resultValues.push(type + ": " + name);
}
Result: FAILED
2.0+