<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Javascript OO: Scope of &#8216;this&#8217; variable when using prototype.js</title>
	<atom:link href="http://blog.evandavey.com/2008/03/javascript-oo-scope-of-this-variable-when-using-prototypejs.html/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.evandavey.com/2008/03/javascript-oo-scope-of-this-variable-when-using-prototypejs.html</link>
	<description>Tech tips, hints, and general musings. PHP, Perl, Mysql, Javascript, AJAX, JSON, Linux, Mac OSX</description>
	<lastBuildDate>Tue, 22 Nov 2011 00:50:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
	<item>
		<title>By: Ventzy</title>
		<link>http://blog.evandavey.com/2008/03/javascript-oo-scope-of-this-variable-when-using-prototypejs.html/comment-page-1#comment-454</link>
		<dc:creator>Ventzy</dc:creator>
		<pubDate>Sat, 06 Sep 2008 20:01:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.evandavey.com/2008/03/javascript-oo-scope-of-this-variable-when-using-prototype.js.html#comment-454</guid>
		<description>Hayley Watson, you saved my day ;)</description>
		<content:encoded><![CDATA[<p>Hayley Watson, you saved my day <img src='http://blog.evandavey.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hayley Watson</title>
		<link>http://blog.evandavey.com/2008/03/javascript-oo-scope-of-this-variable-when-using-prototypejs.html/comment-page-1#comment-15</link>
		<dc:creator>Hayley Watson</dc:creator>
		<pubDate>Mon, 10 Mar 2008 19:57:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.evandavey.com/2008/03/javascript-oo-scope-of-this-variable-when-using-prototype.js.html#comment-15</guid>
		<description>Alternate solutions are to write something like:

callAjax: function() {
	var me = this;
    new Ajax.Request(&#039;ajax.php&#039;, {
        onComplete: function(transport) {
            me.result = transport.responseText.evalJSON();
        }
    }
}

or

callAjax: function() {
    new Ajax.Request(&#039;ajax.php&#039;, {
        onComplete: (function(transport) {
            this.result = transport.responseText.evalJSON();
        }).bind(this);
    }
}

In the former case &quot;me&quot; is set to the callAjax&#039;s containing object and since &quot;me&quot; is not &quot;this&quot; it&#039;s unaffected by onComplete&#039;s bindings. The latter uses Prototype&#039;s bind() method to state that onComplete&#039;s &quot;this&quot; refers to the same object as callAjax&#039;s &quot;this&quot; (effectively by encapsulating the former idiom).

In either case, the advantage is that &quot;myobj&quot; isn&#039;t explicitly mentioned, so it doesn&#039;t need to be globally scoped, and the same method can be aggregated by other objects without them mistakenly messing with myobj&#039;s result.</description>
		<content:encoded><![CDATA[<p>Alternate solutions are to write something like:</p>
<p>callAjax: function() {<br />
	var me = this;<br />
    new Ajax.Request(&#8216;ajax.php&#8217;, {<br />
        onComplete: function(transport) {<br />
            me.result = transport.responseText.evalJSON();<br />
        }<br />
    }<br />
}</p>
<p>or</p>
<p>callAjax: function() {<br />
    new Ajax.Request(&#8216;ajax.php&#8217;, {<br />
        onComplete: (function(transport) {<br />
            this.result = transport.responseText.evalJSON();<br />
        }).bind(this);<br />
    }<br />
}</p>
<p>In the former case &#8220;me&#8221; is set to the callAjax&#8217;s containing object and since &#8220;me&#8221; is not &#8220;this&#8221; it&#8217;s unaffected by onComplete&#8217;s bindings. The latter uses Prototype&#8217;s bind() method to state that onComplete&#8217;s &#8220;this&#8221; refers to the same object as callAjax&#8217;s &#8220;this&#8221; (effectively by encapsulating the former idiom).</p>
<p>In either case, the advantage is that &#8220;myobj&#8221; isn&#8217;t explicitly mentioned, so it doesn&#8217;t need to be globally scoped, and the same method can be aggregated by other objects without them mistakenly messing with myobj&#8217;s result.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

