Skip to content

Instantly share code, notes, and snippets.

@themactep
Forked from maccman/bug.js
Created March 8, 2010 22:39
Show Gist options
  • Save themactep/325888 to your computer and use it in GitHub Desktop.
Save themactep/325888 to your computer and use it in GitHub Desktop.
// Is this a JavaScript bug?
// Running the code below alert's "three".
var calls = ["one", "two", "three"]
for(var i in calls) {
var name = calls[i];
this[name] = function(){
alert(name);
}
}
this.one()
// that's how it actually works:
this['one'] = function() {
alert(name);
}
this['two'] = function() {
alert(name);
}
this['three'] = function() {
alert(name);
}
for (var i in ["one", "two", "three"]) {
var name = calls[i];
}
// let's check the name
console.log(name); // => "three"
this.one(); // "three", of course
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment