allowed-loop-closures.js
2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"use strict";
var arr = [];
// can be transformed (common WAT)
for (var x = 0; x < 10; x++) {
arr.push(function() { return x; });
}
// can be transformed (common manual work-around)
for (let x = 0; x < 3; x++) {
arr.push((function(x) { return function() { return x; } })(x));
}
// can be transformed (no extra IIFE will be inserted)
for (let x = 0; x < 3; x++) {(function(){
let y = 1;
arr.push(function() { return y; });
}).call(this);}
// can be transformed (added IIFE)
for (let x = 0; x < 3; x++) {
let y = 1;
arr.push(function() { return y; });
}
// can be transformed (added IIFE)
for (let x = 0; x < 3; x++) {
let y = x;
arr.push(function() { return y; });
}
// can be transformed (added IIFE)
for (let x = 0; x < 3; x++) {
let y = x, z = arr.push(function() { return y; });
}
// can be transformed (added IIFE)
for (let x = 0; x < 3; x++) {
let x = 1;
arr.push(function() { return x; });
}
// can be transformed (added IIFE)
while (true) {
let f = function() {
for (let x = 0; x < 10; x++) {
let y = x;
arr.push(function() { return y; });
}
};
f();
}
// it's fine to use break, continue, return and arguments as long as
// it's contained within a function below the loop so that it doesn't
// interfere with the inserted IIFE
(function() {
for (let x = 0; x < 3; x++) {
let y = x;
(function() {
for(;;) break;
return;
})();
(function() {
for(;;) continue;
arguments
})();
arr.push(function() { return y; });
}
})();
// For-In
for (let x in [0,1,2]) {
arr.push(function() { return x; });
}
// Block-less For-In
for (let x in [0,1,2]) arr.push(function() { return x; });/*with semicolon*/
for (let x in [0,1,2]) arr.push(function() { return x; })/*no semicolon*/
null; // previous semicolon-less for statement's range ends just before 'n' in 'null'
// For-Of
for (let x of [0,1,2]) {
arr.push(function() { return x; });
}
// Block-less For-Of
for (let x of [0,1,2]) arr.push(function() { return x; });/*with semicolon*/
for (let x of [0,1,2]) arr.push(function() { return x; })/*no semicolon*/
null; // previous semicolon-less for statement's range ends just before 'n' in 'null'
// While
while (true) {
let x = 1;
arr.push(function() { return x; });
}
// Do-While
do {
let x = 1;
arr.push(function() { return x; });
} while (true);
arr.forEach(function(f) {
console.log(f());
});