forked from brianleroux/wtfjs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iteration without explicit variable declaration
Signed-off-by: DTrejo <david.trejo@voxer.com>
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
See if you can guess what this will output: | ||
|
||
<code> | ||
function allNames() { | ||
var names = [ 'dan', 'anthony', 'pavel' ]; | ||
for (name in names) { | ||
console.log(name); | ||
} | ||
} | ||
|
||
allNames(); | ||
</code> | ||
|
||
If you guessed this: | ||
|
||
> 0<br/> | ||
> 1<br/> | ||
> 2<br/> | ||
|
||
...then you are right! Congratulations! | ||
|
||
What you might not have guessed, is what this will output: | ||
|
||
<code> | ||
console.log(window.name); | ||
</code> | ||
|
||
its: | ||
|
||
> "2" | ||
|
||
Iteration is assignment, and without the use of the `var` keyword, you're really using the `this` object - which in the context of a raw function declaration is the `window` object. | ||
|
||
So the original function is equivalent to: | ||
|
||
<code> | ||
function allNames() { | ||
var names = [ 'dan', 'anthony', 'pavel' ]; | ||
for (this.name in names) { | ||
console.log(name); | ||
} | ||
} | ||
</code> | ||
|
||
Which is also equivalent to: | ||
|
||
<code> | ||
function allNames() { | ||
var names = [ 'dan', 'anthony', 'pavel' ]; | ||
for (window.name in names) { | ||
console.log(name); | ||
} | ||
} | ||
</code> | ||
|
||
By [@danlash][1] | ||
|
||
[1]:https://twitter.com/danlash |