<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Caffeinated Simpleton - Latest Comments in An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>http://caffinatedsimpleton.disqus.com/</link><description>My personal blog</description><atom:link href="https://caffinatedsimpleton.disqus.com/an_introduction_to_javascript8217s_8220this8221/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 29 Dec 2015 11:58:28 -0000</lastBuildDate><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-2430763957</link><description>&lt;p&gt;Not only practical, but a fine work of literature too.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">justin</dc:creator><pubDate>Tue, 29 Dec 2015 11:58:28 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-2430262632</link><description>&lt;p&gt;Still the best explanation on the internets&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">chug2k</dc:creator><pubDate>Tue, 29 Dec 2015 03:31:43 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-800665309</link><description>&lt;p&gt;"This" probably is confusing to newbies.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Carton Donofrio</dc:creator><pubDate>Fri, 15 Feb 2013 14:44:16 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-447494877</link><description>&lt;p&gt;I've read many explanations of this, most are very long, none have come close to this one. I now actually feel like I understand it. Many thanks&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">John</dc:creator><pubDate>Thu, 23 Feb 2012 21:41:04 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-418540879</link><description>&lt;p&gt;Very nice tutorial, thank you :-) Do you know if any of these methods is suitable to actionscript too?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrea Padovani</dc:creator><pubDate>Mon, 23 Jan 2012 04:18:33 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-205104541</link><description>&lt;p&gt; Excellent summary. Reading 'The Good Parts' and lots of online examples - none clearer that this one :-)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Dylan Thomas</dc:creator><pubDate>Tue, 17 May 2011 12:27:05 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-114601084</link><description>&lt;p&gt;Good catch!&lt;/p&gt;&lt;p&gt;I recently updated the post to use "self" instead of "my", since more and&lt;br&gt;more people are using that style these days. It's also the style I prefer&lt;br&gt;now. I guess I missed one!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">justin</dc:creator><pubDate>Sat, 18 Dec 2010 21:11:29 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-114598860</link><description>&lt;p&gt;I look at the code above and am confused on one line of code - see below, last line I paste. Where do you get 'return my.condiments' from? should it not be 'return self.condiments' as you note in the comment at the end of that line?&lt;/p&gt;&lt;p&gt;function HotDog() {&lt;br&gt;    var self = this; // self references the current this, which is correct.&lt;br&gt;    self.condiments = "mustard, ketchup";&lt;br&gt;    self.getCondiments = function() {&lt;br&gt;         return my.condiments; // self is guaranteed to be a reference to the original "this"&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Simon</dc:creator><pubDate>Sat, 18 Dec 2010 21:05:09 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-48438511</link><description>&lt;p&gt;Wait, php or javascript? At least javascript has some consistency to it, php... not so much&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Sean Sandy</dc:creator><pubDate>Tue, 04 May 2010 22:14:13 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-39445016</link><description>&lt;p&gt;There are a few more intricacies to 'this'.  I've attempted to address most of them here: &lt;a href="http://lucassmith.name/pub/this.html" rel="nofollow noopener" target="_blank" title="http://lucassmith.name/pub/this.html"&gt;http://lucassmith.name/pub/...&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">ls_n</dc:creator><pubDate>Fri, 12 Mar 2010 18:13:21 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17767180</link><description>&lt;p&gt;Good article. I have been very confused by "this".&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">vilcans</dc:creator><pubDate>Tue, 29 Sep 2009 08:40:23 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17762471</link><description>&lt;p&gt;Variable scope in JavaScript is function. There is no block scope.&lt;br&gt;Defining variable 'a' with a 'var' statement inside the loop is just misleading. JavaScript is not Java.&lt;br&gt;So the two version do the same.&lt;/p&gt;&lt;p&gt;Also you didn't test your code at all.. Else you would have seen that alert is not called by setTimeOut but instead inside the loop.&lt;br&gt;You should have written this:&lt;br&gt;setTimeout(function { alert(i) }, 500);&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Olivier Mengué</dc:creator><pubDate>Tue, 29 Sep 2009 04:24:55 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17743863</link><description>&lt;p&gt;There is a bit more to the setTimeout() function to be totally clear:&lt;/p&gt;&lt;p&gt;1) setTimeout(myObj.myFunc, 1000) - this set to global window object&lt;/p&gt;&lt;p&gt;2) setTimeout(function() { myObj.myFunc() }, 1000) - this set to myObj as we want it.&lt;/p&gt;&lt;p&gt;But you can also pass a string to setTimeout and basically setTimeout() becomes a sort of eval:&lt;/p&gt;&lt;p&gt;3) setTimeout('myObj.myFunc()', 1000) - this set to myObj&lt;/p&gt;&lt;p&gt;Number 2 is the best way to go though.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">hugobryn</dc:creator><pubDate>Mon, 28 Sep 2009 18:11:13 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17719538</link><description>&lt;p&gt;All that stuff looks like PHP. Disgusting language JavaScript, ughhh...&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Marcos</dc:creator><pubDate>Mon, 28 Sep 2009 10:29:27 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17705639</link><description>&lt;p&gt;As was pointed out to me on &lt;a href="http://Stackoverflow.com" rel="nofollow noopener" target="_blank" title="Stackoverflow.com"&gt;Stackoverflow.com&lt;/a&gt;, you can also solve it by using closures &lt;a href="http://stackoverflow.com/questions/1466875/ies-understanding-of-this" rel="nofollow noopener" target="_blank" title="http://stackoverflow.com/questions/1466875/ies-understanding-of-this"&gt;http://stackoverflow.com/qu...&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Sean Sandy</dc:creator><pubDate>Mon, 28 Sep 2009 03:43:27 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17704531</link><description>&lt;p&gt;i used methods introduced in &lt;a href="http://odetocode.com/Blogs/scott/archive/2007/07/05/11072.aspx" rel="nofollow noopener" target="_blank" title="http://odetocode.com/Blogs/scott/archive/2007/07/05/11072.aspx"&gt;http://odetocode.com/Blogs/...&lt;/a&gt; to solve 'this' problems&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jeffrey04</dc:creator><pubDate>Mon, 28 Sep 2009 02:34:00 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17644993</link><description>&lt;p&gt;Updated the first example. Thanks!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">justin</dc:creator><pubDate>Sun, 27 Sep 2009 16:32:11 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17644914</link><description>&lt;p&gt;Yeah, I think you're right DougBTX. I've changed it.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">justin</dc:creator><pubDate>Sun, 27 Sep 2009 16:28:28 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17644717</link><description>&lt;p&gt;You're right that it's unnecessary. I was trying to combine the previous point, but it might just make things more confusing. I might change it.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">justin</dc:creator><pubDate>Sun, 27 Sep 2009 16:18:56 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17644232</link><description>&lt;p&gt;In the "solving our setTimeout problem" code sample, why do you use apply? At this point you have a reference to the object, so you can call myHotDog.getCondiments() directly. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">DougBTX</dc:creator><pubDate>Sun, 27 Sep 2009 15:59:31 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17635169</link><description>&lt;p&gt;Using:&lt;/p&gt;&lt;p&gt;var my = this;&lt;/p&gt;&lt;p&gt;Is such a beautiful fix, I'm amazed by its simplicity. Cheers!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lachlan</dc:creator><pubDate>Sun, 27 Sep 2009 09:35:11 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17635076</link><description>&lt;p&gt;It will work if you call it once in a function-block.&lt;br&gt;The scope of the timeout-callback will be the same as where it was called.&lt;br&gt;try this:&lt;br&gt;for(var i=0;i&amp;lt;3;i++)&lt;br&gt;{&lt;br&gt;   setTimeout(alert(i), 500); //Will write 3, 3 times because i is 3 when alert is called&lt;br&gt;}&lt;/p&gt;&lt;p&gt;vs&lt;/p&gt;&lt;p&gt;for(var i=0;i&amp;lt;3;i++)&lt;br&gt;{&lt;br&gt;   var a = i;&lt;br&gt;   setTimeout(a, 500); //Will write 0, 1, 2 because a is only within the scope of the current iteration&lt;br&gt;}&lt;/p&gt;&lt;p&gt;Google closures for more info&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">budda</dc:creator><pubDate>Sun, 27 Sep 2009 09:27:22 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17635013</link><description>&lt;p&gt;Ok, I was wrong :-) when doing alert(this.condiments) instead it says "undefined". Now I also see why setting var me = this; helps.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">mspreij</dc:creator><pubDate>Sun, 27 Sep 2009 09:22:39 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17634594</link><description>&lt;p&gt;You still have to fix it in the first example (under "this Gets Tricky").&lt;br&gt;Also, the following works just fine for me, in various browsers (IE7/8, FF)? No errors on the console and it happily alerts after 3 seconds.&lt;/p&gt;&lt;p&gt;function HotDog() {&lt;br&gt;  this.condiments = "mustard, ketchup";&lt;br&gt;  this.getCondiments = function() {&lt;br&gt;    alert(1);&lt;br&gt;    return this.condiments;&lt;br&gt;  }&lt;br&gt;}&lt;/p&gt;&lt;p&gt;var myHotDog = new HotDog();&lt;/p&gt;&lt;p&gt;setTimeout(myHotDog.getCondiments, 3000);&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">MSpreij</dc:creator><pubDate>Sun, 27 Sep 2009 08:45:37 -0000</pubDate></item><item><title>Re: An Introduction to JavaScript&amp;#8217;s &amp;#8220;this&amp;#8221;</title><link>https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html#comment-17627819</link><description>&lt;p&gt;You're right about the order, it's been corrected. I don't really believe in putting millisecond timeouts in examples since it's needlessly detail oriented. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">justin</dc:creator><pubDate>Sun, 27 Sep 2009 06:55:41 -0000</pubDate></item></channel></rss>