Skip to content

Instantly share code, notes, and snippets.

@geraldalewis
Created January 31, 2012 18:03
Show Gist options
  • Save geraldalewis/1711902 to your computer and use it in GitHub Desktop.
Save geraldalewis/1711902 to your computer and use it in GitHub Desktop.
Emulating `let`
var print = console.log;
var header = function(name){print("\n>>"+ name.toUpperCase() +"<<")}
var subheader = function(name){print(" >"+ name +"< ")}
header("iterating arrays");
subheader("emulated let");
(function() {
var callbacks = [];
for (var i = 0; i < 3; i++) {
try{ throw i }
catch (i){
// `catch` variables are block scoped
callbacks.push(function() {
return console.log(' ' + i);
});
}
}
// code might rely on the loop variable after the loop exits
// for instance, when finding the index of an element
print(' Loop variable should be "3": ' + (i === 3));// true
for (var j = 0; j <= 2; j++) callbacks[j](); // 0,1,2
}());
subheader("traditional, naive way");
(function() {
var callbacks = [];
for (var i = 0; i < 3; i++) {
callbacks.push(function() {
return console.log(' ' + i);
});
}
print(' Loop variable should be "3": ' + (i === 3));// true
for (var j = 0; j <= 2; j++) callbacks[j](); // 3,3,3
}());
header("iterating objects ");
subheader("emulated let");
(function() {
var o = {
key1: 'val 1',
key2: 'val 2',
key3: 'val 3'
};
var callbacks = [];
for (var key in o) {
// both the key and value can be captured by
// nesting each in their own try..catch block
try{ throw key}
catch(key){
try{ throw o[key] }
catch(val){
callbacks.push(function(){
console.log(' ' + key + ": " + val);
});
}
}
}
print(' Loop variable should be "key3": ' + (key === 'key3')); // true
for (var j = 0; j <= 2; j++) callbacks[j]();
/*
key1: val 1
key2: val 2
key3: val 3
*/
}());
subheader("traditional, naive way");
(function() {
var o = {
key1: 'val 1',
key2: 'val 2',
key3: 'val 3'
};
var callbacks = [];
var val;
for (var key in o) {
val = o[key];
callbacks.push(function(){
console.log( ' ' + key + ": " + val);
});
}
print(' Loop variable should be "key3": ' + (key === 'key3')); // true
for (var j = 0; j <= 2; j++) callbacks[j]();
/*
key3: val 3
key3: val 3
key3: val 3
*/
}());
header("control flow");
(function() {
var callbacks = [];
for (var i = 0; i < 3; i++) {
try{ throw i }
catch (i){
callbacks.push(function() {
return console.log(' ' + i);
});
break;
}
}
print(' Loop variable should be "0": ' + (i === 0)); // true
for (var j = 0; j <= 2; j++)
typeof callbacks[j] === 'function' && callbacks[j](); // 0
}());
subheader("mutable indices");
(function() {
var callbacks = [];
for (var i = 0, j = 0; i < 3; i++) {
try{ throw i }
catch (i){
// `catch` variables are block scoped
++i;
callbacks.push(function() {
return console.log(' ' + i);
});
j = i;
}
i = j;
}
// code might rely on the loop variable after the loop exits
// for instance, when finding the index of an element
print(' Loop variable should be "4": ' + (i === 4));// true
for (var k = 0; k <= 2; k++)
typeof callbacks[k] === 'function' && callbacks[k](); // 1,3
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment