Skip to content

Instantly share code, notes, and snippets.

@whizzter
Last active January 26, 2016 09:19
Show Gist options
  • Save whizzter/d7e721fbf89253fdd59a to your computer and use it in GitHub Desktop.
Save whizzter/d7e721fbf89253fdd59a to your computer and use it in GitHub Desktop.
Var and let
<!-- find the bug -->
<html>
<head>
<script>
"use strict";
function iny() {
let body=document.getElementById("bod");
let nums=[1,2,3];
for (let lnum of nums) {
let but=mkButton("Let button"+lnum);
but.onclick=function() {
alert("Clicked "+lnum);
}
}
for (var vnum of nums) {
var but=mkButton("Var button "+vnum);
but.onclick=function() {
alert("Clicked "+vnum);
}
}
function mkButton(name) {
var but=document.createElement("input");
but.type="button";
but.value=name;
body.appendChild(but);
return but;
}
}
</script>
</head>
<body onload="iny();" id="bod">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment