<?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"
	>
<channel>
	<title>Comments on: Delegates in JS</title>
	<atom:link href="http://erik.eae.net/archives/2005/02/10/22.26.03/feed/" rel="self" type="application/rss+xml" />
	<link>http://erik.eae.net/archives/2005/02/10/22.26.03/</link>
	<description>The Weblog of Erik Arvidsson</description>
	<pubDate>Wed, 07 Jan 2009 12:38:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Simonboris</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-16480</link>
		<dc:creator>Simonboris</dc:creator>
		<pubDate>Thu, 08 Jun 2006 01:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-16480</guid>
		<description>if(!Function.prototype.apply)
{
	Function.prototype.apply = function(o,a)
	{
		var r, e;
		if(!o) var o 	= window;
		if(!a) var a 	= new Array();
		var b 		= new Array();
		for(var i=0;i&lt;a&gt;</description>
		<content:encoded><![CDATA[<p>if(!Function.prototype.apply)<br />
{<br />
	Function.prototype.apply = function(o,a)<br />
	{<br />
		var r, e;<br />
		if(!o) var o 	= window;<br />
		if(!a) var a 	= new Array();<br />
		var b 		= new Array();<br />
		for(var i=0;i<a></a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simonboris</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-16479</link>
		<dc:creator>Simonboris</dc:creator>
		<pubDate>Thu, 08 Jun 2006 01:31:42 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-16479</guid>
		<description>For those who refuse to use the Function.apply because old browser do not support it, copy paste this in you js files.

If Function.apply is not available in a browser, this will be run and it do the smae thing:

if(!Function.prototype.apply)
{
	Function.prototype.apply = function(o,a)
	{
		var r, e;
		if(!o) var o 	= window;
		if(!a) var a 	= new Array();
		var b 		= new Array();
		for(var i=0;i&lt;a&gt;</description>
		<content:encoded><![CDATA[<p>For those who refuse to use the Function.apply because old browser do not support it, copy paste this in you js files.</p>
<p>If Function.apply is not available in a browser, this will be run and it do the smae thing:</p>
<p>if(!Function.prototype.apply)<br />
{<br />
	Function.prototype.apply = function(o,a)<br />
	{<br />
		var r, e;<br />
		if(!o) var o 	= window;<br />
		if(!a) var a 	= new Array();<br />
		var b 		= new Array();<br />
		for(var i=0;i<a></a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: protoscript</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1212</link>
		<dc:creator>protoscript</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1212</guid>
		<description>That's funny Erik... I had just extended Object in the same way about two weeks ago. As always, I am jealous of your coding style (and your writing style, esp since English is not your native language!). I really enjoyed State of the Nation, too.

Hope to speak to you soon, my friend.</description>
		<content:encoded><![CDATA[<p>That&#8217;s funny Erik&#8230; I had just extended Object in the same way about two weeks ago. As always, I am jealous of your coding style (and your writing style, esp since English is not your native language!). I really enjoyed State of the Nation, too.</p>
<p>Hope to speak to you soon, my friend.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Arvidsson</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1213</link>
		<dc:creator>Erik Arvidsson</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1213</guid>
		<description>There are of course serious draw backs to extending Object and I would not do it in a real world application because it breaks the usage of Objects as hash tables. Therefore the other alternative is better (making it a static method of Function might make more sense).

The reason I posted this today was becasue I saw that they were doing something similar in Google maps.

Object.prototype.eventHandler = function (sMeth) {
   var self = this;
   return function (e) {
      if (!e) e = window.event;
      if (!e.target) e = e.srcElement;
      // I would add preventDefault and stopPropagation as well
      return self[sMeth](e);
   };
};</description>
		<content:encoded><![CDATA[<p>There are of course serious draw backs to extending Object and I would not do it in a real world application because it breaks the usage of Objects as hash tables. Therefore the other alternative is better (making it a static method of Function might make more sense).</p>
<p>The reason I posted this today was becasue I saw that they were doing something similar in Google maps.</p>
<p>Object.prototype.eventHandler = function (sMeth) {<br />
   var self = this;<br />
   return function (e) {<br />
      if (!e) e = window.event;<br />
      if (!e.target) e = e.srcElement;<br />
      // I would add preventDefault and stopPropagation as well<br />
      return self[sMeth](e);<br />
   };<br />
};</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dean Edwards</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1214</link>
		<dc:creator>Dean Edwards</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1214</guid>
		<description>&lt;p&gt;Good tip Erik. I'll point out that IE5.0 does not support &lt;i&gt;Function&lt;/i&gt;.apply. But many people have stopped supporting this browser (even MS themselves on the new MSN Serach). So maybe this is not an issue. Except that it still has more share than Opera and Safari...&lt;/p&gt;
&lt;p&gt;Keep posting these tips btw. As protoscript says, you have a clear writing and coding style.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Good tip Erik. I&#8217;ll point out that IE5.0 does not support <i>Function</i>.apply. But many people have stopped supporting this browser (even MS themselves on the new MSN Serach). So maybe this is not an issue. Except that it still has more share than Opera and Safari&#8230;</p>
<p>Keep posting these tips btw. As protoscript says, you have a clear writing and coding style.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Trenka</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1215</link>
		<dc:creator>Tom Trenka</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1215</guid>
		<description>I try to not "tout my own horn", as it were, but you might want to take a look at the Events system I set up in f(m) (http://fm.dept-z.com/index.asp?get=/Resources/Reference/System/Events  is a decent place to start)...it's modeled directly on the .NET delegate system, and solves this problem by forcing the event handler in question to take 2 arguments instead of one--the object that fired the event, and an event arguments object.

Nice thing about it is that 1.  it can be applied to anything and 2. it preserves the original scope in a way that is very clear.

Thought you might be interested.</description>
		<content:encoded><![CDATA[<p>I try to not &#8220;tout my own horn&#8221;, as it were, but you might want to take a look at the Events system I set up in f(m) (http://fm.dept-z.com/index.asp?get=/Resources/Reference/System/Events  is a decent place to start)&#8230;it&#8217;s modeled directly on the .NET delegate system, and solves this problem by forcing the event handler in question to take 2 arguments instead of one&#8211;the object that fired the event, and an event arguments object.</p>
<p>Nice thing about it is that 1.  it can be applied to anything and 2. it preserves the original scope in a way that is very clear.</p>
<p>Thought you might be interested.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Arvidsson</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1216</link>
		<dc:creator>Erik Arvidsson</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1216</guid>
		<description>&lt;p&gt;Tom: I looked at f(m) a week or so back. It is very nice. Yesterday I also saw dojo. Also nice. I'll try to comment on that at a later time.&lt;/p&gt;

&lt;p&gt;For commercial APIs I recommend using what you are suggesting or doing what we are doing in Bindows.&lt;/p&gt;

&lt;p&gt;Bindows:&lt;/p&gt;

&lt;p&gt;this.obj.addEventLsitener("foo", this.handleFoo, this)&lt;/p&gt;

&lt;p&gt;f(m)?&lt;/p&gt;

&lt;p&gt;this.obj.OnFoo.Add(new Events.EventHandler(this.handleFoo));&lt;/p&gt;

&lt;p&gt;The documentation was a bit confusing here. I could not read out how to do it in the right way to get this.handleFoo working as expected. You say that it takes 2 arguments so then I hope it would finally end up something like this:&lt;/p&gt;

&lt;p&gt;this.obj.OnFoo.Add(this.handleFoo, this)&lt;/p&gt;

&lt;p&gt;The good thing with this over the W3C/Bindows model is that the events can be discovered more easily. The bad thing is that custom events require an interface change.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Tom: I looked at f(m) a week or so back. It is very nice. Yesterday I also saw dojo. Also nice. I&#8217;ll try to comment on that at a later time.</p>
<p>For commercial APIs I recommend using what you are suggesting or doing what we are doing in Bindows.</p>
<p>Bindows:</p>
<p>this.obj.addEventLsitener(&#8221;foo&#8221;, this.handleFoo, this)</p>
<p>f(m)?</p>
<p>this.obj.OnFoo.Add(new Events.EventHandler(this.handleFoo));</p>
<p>The documentation was a bit confusing here. I could not read out how to do it in the right way to get this.handleFoo working as expected. You say that it takes 2 arguments so then I hope it would finally end up something like this:</p>
<p>this.obj.OnFoo.Add(this.handleFoo, this)</p>
<p>The good thing with this over the W3C/Bindows model is that the events can be discovered more easily. The bad thing is that custom events require an interface change.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Trenka</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1217</link>
		<dc:creator>Tom Trenka</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1217</guid>
		<description>Hmm.  It looks like my quick docs suck then :)

Here's the deal:  *your* function takes two args. So:

function myHandler(src, ea) { 
// do something
}

this.obj.OnFoo.Add(new Events.EventHandler(myHandler))

...like that.  I suppose I should really go through and make that clearer...</description>
		<content:encoded><![CDATA[<p>Hmm.  It looks like my quick docs suck then <img src='http://erik.eae.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Here&#8217;s the deal:  *your* function takes two args. So:</p>
<p>function myHandler(src, ea) {<br />
// do something<br />
}</p>
<p>this.obj.OnFoo.Add(new Events.EventHandler(myHandler))</p>
<p>&#8230;like that.  I suppose I should really go through and make that clearer&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Arvidsson</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1218</link>
		<dc:creator>Erik Arvidsson</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1218</guid>
		<description>&lt;p&gt;Tom: Isn't the src argument same as event.target (in DOM)?&lt;/p&gt;

&lt;pre&gt;a.b.OnFoo.Add(new Events.EventHandler(a.handleFoo))&lt;/pre&gt;

&lt;p&gt;How would I get to a in a.handleFoo (when called from the event handler)? I get the impression that src would point to a.b? At least that is how .NET does it.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Tom: Isn&#8217;t the src argument same as event.target (in DOM)?</p>
<pre>a.b.OnFoo.Add(new Events.EventHandler(a.handleFoo))</pre>
<p>How would I get to a in a.handleFoo (when called from the event handler)? I get the impression that src would point to a.b? At least that is how .NET does it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Trenka</title>
		<link>http://erik.eae.net/archives/2005/02/10/22.26.03/#comment-1219</link>
		<dc:creator>Tom Trenka</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://erik.eae.net/wp/?p=116#comment-1219</guid>
		<description>If you're attaching the event handler to a DOM object, yes, it is the equivilent of target, absolutely.

The difference is that the event handler is executed in the scope of "a" in your example, *not* in the scope of "b"; in this particular case, the src arg of your event handler *should* be a reference to "b".  So yes, it works very much like the way .NET does (which was the point, a lot of the aim of f(m) is to bring some of the cleanliness of the .NET framework to JS).

The big difference with what I've done is that it's a unified model; it works the same whether or not you are firing custom events on your own objects, or you've attached them to DOM objects.  Which is pretty nice.  I was kind of impressed with myself when I was able to pull something like this:

document.getElementById("myFoo").onclick = (new Events.Listener()).Invoke ;
document.getElementById("myFoo").onclick.Add(new Events.MouseEventHandler(myHandlerFunc)) ;

:)</description>
		<content:encoded><![CDATA[<p>If you&#8217;re attaching the event handler to a DOM object, yes, it is the equivilent of target, absolutely.</p>
<p>The difference is that the event handler is executed in the scope of &#8220;a&#8221; in your example, *not* in the scope of &#8220;b&#8221;; in this particular case, the src arg of your event handler *should* be a reference to &#8220;b&#8221;.  So yes, it works very much like the way .NET does (which was the point, a lot of the aim of f(m) is to bring some of the cleanliness of the .NET framework to JS).</p>
<p>The big difference with what I&#8217;ve done is that it&#8217;s a unified model; it works the same whether or not you are firing custom events on your own objects, or you&#8217;ve attached them to DOM objects.  Which is pretty nice.  I was kind of impressed with myself when I was able to pull something like this:</p>
<p>document.getElementById(&#8221;myFoo&#8221;).onclick = (new Events.Listener()).Invoke ;<br />
document.getElementById(&#8221;myFoo&#8221;).onclick.Add(new Events.MouseEventHandler(myHandlerFunc)) ;<br />
 <img src='http://erik.eae.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
</channel>
</rss>
