Skip to content

Instantly share code, notes, and snippets.

@lukasz-zak
Created April 29, 2014 22:21
Show Gist options
  • Save lukasz-zak/18dfbd7a4e2a34011cd9 to your computer and use it in GitHub Desktop.
Save lukasz-zak/18dfbd7a4e2a34011cd9 to your computer and use it in GitHub Desktop.
Ecmascript6 - let examples
(function(){
"use strict"
var case1 = function(){
console.log("foo", foo);
if(true){
var foo = 'foo';
console.log("foo", foo);
}
console.log("foo", foo);
};
var case2 = function(){
console.log("bar", bar);
if(true){
let bar = 'bar';
console.log("bar", bar);
}
console.log("bar", bar);
};
var case3 = function(){
for (var i = 0; i < 10; i++) {
var btn = document.createElement('button');
btn.textContent = "Element with number " + i;
btn.addEventListener('click', function(){
console.log("button number is:", i);
})
document.body.appendChild(btn);
document.body.appendChild(document.createElement('br'));
};
};
var case4 = function(){
for (var i = 0; i < 10; i++) {
var btn = document.createElement('button');
btn.textContent = "Another element with number " + i;
let j = i;
btn.addEventListener('click', function(){
console.log("button number is:", j);
})
document.body.appendChild(btn);
document.body.appendChild(document.createElement('br'));
};
};
case1();
//Remember that case2() cause a error and prevent parsing rest of script
//case2();
case3();
case4();
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment