<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: JavaScript Events</title>
	<link>http://missig.org/julian/blog/2005/06/22/javascript-events/</link>
	<description>Former Open Source programmer with experience at companies like IBM and Apple. Now a UI Designer with an education in Cognitive Science and Human-Computer Interaction.</description>
	<pubDate>Sat, 22 Nov 2008 13:25:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: Patrick</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-97274</link>
		<dc:creator>Patrick</dc:creator>
		<pubDate>Sun, 17 Feb 2008 11:25:37 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-97274</guid>
		<description>Mihai's solution didn't work for me, but ben's did! Nice work there!

I'm so relieved someone else was having this problem. I thought my debugger was broken since it displayed the wrong reference for this! :)</description>
		<content:encoded><![CDATA[<p>Mihai&#8217;s solution didn&#8217;t work for me, but ben&#8217;s did! Nice work there!</p>
<p>I&#8217;m so relieved someone else was having this problem. I thought my debugger was broken since it displayed the wrong reference for this! :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ben</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8251</link>
		<dc:creator>ben</dc:creator>
		<pubDate>Thu, 21 Jul 2005 09:22:36 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8251</guid>
		<description>Create a method reference passing in the "object" you wish to refer to as "this" in the event handler

Eg.

In Constructor:
this.oText.onblur = createMethodReference(this, "TextBox_OnBlur");

function createMethodReference(object, methodName) {
    return function () {
        object[methodName].apply(object, arguments);
    };
}

DropDownSelection.prototype.TextBox_OnBlur = function()
{
	var e = window.event;
	if(this.isDropDownOpen  &#38;&#38; !this.isMouseOverDropDown)
		this.Reset();
}

"this" now refers to my object in the event handler, not the calling object.</description>
		<content:encoded><![CDATA[<p>Create a method reference passing in the &#8220;object&#8221; you wish to refer to as &#8220;this&#8221; in the event handler</p>
<p>Eg.</p>
<p>In Constructor:<br />
this.oText.onblur = createMethodReference(this, &#8220;TextBox_OnBlur&#8221;);</p>
<p>function createMethodReference(object, methodName) {<br />
    return function () {<br />
        object[methodName].apply(object, arguments);<br />
    };<br />
}</p>
<p>DropDownSelection.prototype.TextBox_OnBlur = function()<br />
{<br />
	var e = window.event;<br />
	if(this.isDropDownOpen  &amp;&amp; !this.isMouseOverDropDown)<br />
		this.Reset();<br />
}</p>
<p>&#8220;this&#8221; now refers to my object in the event handler, not the calling object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: julian</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8249</link>
		<dc:creator>julian</dc:creator>
		<pubDate>Tue, 19 Jul 2005 18:26:21 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8249</guid>
		<description>Mihai's solution is a bit better for the circumstances I'm dealing with, since I have a lot of different event handlers. It's always good to have options, though. Thanks!</description>
		<content:encoded><![CDATA[<p>Mihai&#8217;s solution is a bit better for the circumstances I&#8217;m dealing with, since I have a lot of different event handlers. It&#8217;s always good to have options, though. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Al Vazquez</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8248</link>
		<dc:creator>Al Vazquez</dc:creator>
		<pubDate>Tue, 19 Jul 2005 14:19:02 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8248</guid>
		<description>I've found this problem as well, and my solution was to create closures. Example:

&lt;code&gt;
function foo() {
  var elementID = "x"; // Somehow provide a reference to the HTMLElement

  function handleClick(){
   alert(this.elementID);
  };
}

bar = new foo();
element = getElementById(bar.elementID);
element.prep =
function(obj) {
  return function() {
    obj.handleClick();
  };
};
element.onclick = element.prep(bar);
&lt;/code&gt;

This creates binds a closure to the HTMLEvent that sends the handleClick() message to the object you pass into prep(). You can reuse the variables "bar" and "element" all you like and it won't tamper with the closures. So far it's been working for me.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve found this problem as well, and my solution was to create closures. Example:</p>
<p><code><br />
function foo() {<br />
  var elementID = "x"; // Somehow provide a reference to the HTMLElement</p>
<p>  function handleClick(){<br />
   alert(this.elementID);<br />
  };<br />
}</p>
<p>bar = new foo();<br />
element = getElementById(bar.elementID);<br />
element.prep =<br />
function(obj) {<br />
  return function() {<br />
    obj.handleClick();<br />
  };<br />
};<br />
element.onclick = element.prep(bar);<br />
</code></p>
<p>This creates binds a closure to the HTMLEvent that sends the handleClick() message to the object you pass into prep(). You can reuse the variables &#8220;bar&#8221; and &#8220;element&#8221; all you like and it won&#8217;t tamper with the closures. So far it&#8217;s been working for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Prather</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8130</link>
		<dc:creator>Daniel Prather</dc:creator>
		<pubDate>Thu, 23 Jun 2005 23:58:54 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8130</guid>
		<description>Just as a point of interest, the Synchronet BBS package (for those that remember the BBS days) now uses JavaScript to create BBS modules.  The software is GPLed and still under active development.  You can check it out at http://www.synchro.net.  The author originally made his own programming language years ago to handle the BBS modules (the language was called Baja), but began redoing everything with JavaScript recently.  It really is pretty cool. :)</description>
		<content:encoded><![CDATA[<p>Just as a point of interest, the Synchronet BBS package (for those that remember the BBS days) now uses JavaScript to create BBS modules.  The software is GPLed and still under active development.  You can check it out at <a href="http://www.synchro.net." rel="nofollow"></a><a href='http://www.synchro.net'>http://www.synchro.net</a>.  The author originally made his own programming language years ago to handle the BBS modules (the language was called Baja), but began redoing everything with JavaScript recently.  It really is pretty cool. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: julian</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8128</link>
		<dc:creator>julian</dc:creator>
		<pubDate>Thu, 23 Jun 2005 18:23:51 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8128</guid>
		<description>It looks like if I want to do serious event work in JavaScript I should be creating my own Event object rather than using the "HTMLEvents" object. After doing that it seems I'll have to see what &lt;code&gt;this&lt;/code&gt; refers to in that situation.</description>
		<content:encoded><![CDATA[<p>It looks like if I want to do serious event work in JavaScript I should be creating my own Event object rather than using the &#8220;HTMLEvents&#8221; object. After doing that it seems I&#8217;ll have to see what <code>this</code> refers to in that situation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: julian</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8107</link>
		<dc:creator>julian</dc:creator>
		<pubDate>Wed, 22 Jun 2005 23:05:23 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8107</guid>
		<description>Aha, that is a slightly better hack around it than what I was doing, yes.

It still kind of sucks to have to do that for every event handler, but it does keep the majority of my code out of the constructor. It's still not an actual solution, but it's a step above what I was doing before. Thanks.</description>
		<content:encoded><![CDATA[<p>Aha, that is a slightly better hack around it than what I was doing, yes.</p>
<p>It still kind of sucks to have to do that for every event handler, but it does keep the majority of my code out of the constructor. It&#8217;s still not an actual solution, but it&#8217;s a step above what I was doing before. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mihai Parparita</title>
		<link>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8106</link>
		<dc:creator>Mihai Parparita</dc:creator>
		<pubDate>Wed, 22 Jun 2005 22:52:38 +0000</pubDate>
		<guid>http://missig.org/julian/blog/2005/06/22/javascript-events/#comment-8106</guid>
		<description>Assuming your constructor is for an object Foo, and at some point in your file you define &lt;code&gt;Foo.prototoype.handleClick&lt;/code&gt;, is there a reason why you can't have this:

&lt;code&gt;function Foo() {
  var self = this;
  var node = ...

  node.onclick = function(event) {self.handleClick(event);}
}

Foo.prototype.handleClick = function(event) {
  ...
}&lt;/code&gt;

Also, look into the &lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt; methods on function objects, they're handy.</description>
		<content:encoded><![CDATA[<p>Assuming your constructor is for an object Foo, and at some point in your file you define <code>Foo.prototoype.handleClick</code>, is there a reason why you can&#8217;t have this:</p>
<p><code>function Foo() {<br />
  var self = this;<br />
  var node = ...</p>
<p>  node.onclick = function(event) {self.handleClick(event);}<br />
}</p>
<p>Foo.prototype.handleClick = function(event) {<br />
  ...<br />
}</code></p>
<p>Also, look into the <code>call()</code> and <code>apply()</code> methods on function objects, they&#8217;re handy.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
