MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/192tn4/common_javascript_gotchas/c8kfw4w/?context=3
r/javascript • u/stevekwan • Feb 23 '13
50 comments sorted by
View all comments
Show parent comments
2
I'd say you should never have an implied global. If you're going to attach something to global, make it clear that you're doing it on purpose:
(function (global) { global.foo = 'foo'; })(window);
Also, something that he doesn't note, but that's important if you're writing production code, is that implied globals (like his examples) are syntax errors in strict mode.
0 u/mirion Feb 23 '13 This. Definitely this. 2 u/rabidcow Feb 23 '13 Sure, you could use this:(function(){ this.foo = 'foo'; })(); 2 u/mirion Feb 23 '13 Sigh. I should know better than to use the word 'this' in a javascript thread. 4 u/rabidcow Feb 23 '13 this in JavaScript threads does not behave the way you would expect. 2 u/rq60 Feb 23 '13 Use 'that' next time. 1 u/[deleted] Feb 24 '13 You should know better than to use the word 'this' in any thread. There's an up arrrow button next to the post that does that.
0
This. Definitely this.
2 u/rabidcow Feb 23 '13 Sure, you could use this:(function(){ this.foo = 'foo'; })(); 2 u/mirion Feb 23 '13 Sigh. I should know better than to use the word 'this' in a javascript thread. 4 u/rabidcow Feb 23 '13 this in JavaScript threads does not behave the way you would expect. 2 u/rq60 Feb 23 '13 Use 'that' next time. 1 u/[deleted] Feb 24 '13 You should know better than to use the word 'this' in any thread. There's an up arrrow button next to the post that does that.
Sure, you could use this:
this
(function(){ this.foo = 'foo'; })();
2 u/mirion Feb 23 '13 Sigh. I should know better than to use the word 'this' in a javascript thread. 4 u/rabidcow Feb 23 '13 this in JavaScript threads does not behave the way you would expect. 2 u/rq60 Feb 23 '13 Use 'that' next time. 1 u/[deleted] Feb 24 '13 You should know better than to use the word 'this' in any thread. There's an up arrrow button next to the post that does that.
Sigh. I should know better than to use the word 'this' in a javascript thread.
4 u/rabidcow Feb 23 '13 this in JavaScript threads does not behave the way you would expect. 2 u/rq60 Feb 23 '13 Use 'that' next time. 1 u/[deleted] Feb 24 '13 You should know better than to use the word 'this' in any thread. There's an up arrrow button next to the post that does that.
4
this in JavaScript threads does not behave the way you would expect.
Use 'that' next time.
1
You should know better than to use the word 'this' in any thread. There's an up arrrow button next to the post that does that.
2
u/kmillns Feb 23 '13
I'd say you should never have an implied global. If you're going to attach something to global, make it clear that you're doing it on purpose:
Also, something that he doesn't note, but that's important if you're writing production code, is that implied globals (like his examples) are syntax errors in strict mode.