Skip to content

Commit

Permalink
adding ES6 let vs var with closures
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Mar 25, 2015
1 parent a51bfc1 commit e9efee9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
46 changes: 46 additions & 0 deletions extras/letWithClosures.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!doctype html>
<html>
<head>
<title>Let, scope, and closures</title>
<meta charset="utf-8">
<style>
div {
padding: 5px;
margin: 5px;
border: 1px solid black;
background-color: lightgray;
width: 100px;
cursor: pointer;
}
</style>
<script src="https://traceur-compiler.googlecode.com/git/bin/traceur.js"
type="text/javascript"></script>
<script src="https://traceur-compiler.googlecode.com/git/src/bootstrap.js"
type="text/javascript"></script>
<script>
traceur.options.experimental = true;
</script>
<script type="module">
window.onload = function() {
var button = document.getElementById("button");
button.onclick = function() {
var body = document.querySelector("body");
for (let i = 0; i < 3; i++) {
var div = document.createElement("div");
div.id = "div" + i;
div.innerHTML = "This is div " + i;
div.onclick = function() {
console.log("You just clicked div " + i);
};
body.appendChild(div);
}
};
};
</script>
</head>
<body>
<h1>let, scope, and closures</h1>
<button id="button">click me</button>
</body>
</html>

46 changes: 46 additions & 0 deletions extras/varWithClosures.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!doctype html>
<html>
<head>
<title>Let, scope, and closures</title>
<meta charset="utf-8">
<style>
div {
padding: 5px;
margin: 5px;
border: 1px solid black;
background-color: lightgray;
width: 100px;
cursor: pointer;
}
</style>
<script src="https://traceur-compiler.googlecode.com/git/bin/traceur.js"
type="text/javascript"></script>
<script src="https://traceur-compiler.googlecode.com/git/src/bootstrap.js"
type="text/javascript"></script>
<script>
traceur.options.experimental = true;
</script>
<script type="module">
window.onload = function() {
var button = document.getElementById("button");
button.onclick = function() {
var body = document.querySelector("body");
for (var i = 0; i < 3; i++) {
var div = document.createElement("div");
div.id = "div" + i;
div.innerHTML = "This is div " + i;
div.onclick = function() {
console.log("You just clicked div " + i);
};
body.appendChild(div);
}
};
};
</script>
</head>
<body>
<h1>let, scope, and closures</h1>
<button id="button">click me</button>
</body>
</html>

0 comments on commit e9efee9

Please sign in to comment.