<?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: Is this a Safari bug?</title>
	<atom:link href="http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/</link>
	<description></description>
	<pubDate>Tue, 07 Oct 2008 10:04:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Jay</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-283517</link>
		<dc:creator>Jay</dc:creator>
		<pubDate>Fri, 22 Jun 2007 16:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-283517</guid>
		<description>Looks like Safari might doing this:
&lt;code&gt;
var theEvent = img.onload;
theEvent();
&lt;/code&gt;

When it should be doing this:
&lt;code&gt;
var theEvent = img.onload;
theEvent.call(img);
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Looks like Safari might doing this:<br />
<code>
var theEvent = img.onload;
theEvent();
</code></p>
<p>When it should be doing this:<br />
<code>
var theEvent = img.onload;
theEvent.call(img);
</code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Lucian</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-109242</link>
		<dc:creator>Lucian</dc:creator>
		<pubDate>Tue, 28 Nov 2006 06:05:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-109242</guid>
		<description>I agree that this is a safari bug, as in all other browsers behaves correctly.</description>
		<content:encoded><![CDATA[<p>I agree that this is a safari bug, as in all other browsers behaves correctly.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Density</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-50572</link>
		<dc:creator>Density</dc:creator>
		<pubDate>Sun, 03 Sep 2006 02:22:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-50572</guid>
		<description>Oh. WHoops. Closure, right</description>
		<content:encoded><![CDATA[<p>Oh. WHoops. Closure, right</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Density</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-50571</link>
		<dc:creator>Density</dc:creator>
		<pubDate>Sun, 03 Sep 2006 02:08:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-50571</guid>
		<description>Wow - that was way too much to wade through because somebody didn't know that "global" functions are methods of the window object.  Troll?

Anyway, did anyone figure out a workaround for this? I've noticed that Safari also returns an onload event for broken images :( and I can't reference reference it's properties because of this bug.</description>
		<content:encoded><![CDATA[<p>Wow - that was way too much to wade through because somebody didn&#8217;t know that &#8220;global&#8221; functions are methods of the window object.  Troll?</p>
<p>Anyway, did anyone figure out a workaround for this? I&#8217;ve noticed that Safari also returns an onload event for broken images :( and I can&#8217;t reference reference it&#8217;s properties because of this bug.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: hax</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-15616</link>
		<dc:creator>hax</dc:creator>
		<pubDate>Tue, 14 Mar 2006 18:19:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-15616</guid>
		<description>"You are drawing a conclusion based on an initial assumption that you are correct." -- omnicity, this sentence should apply to yourself.</description>
		<content:encoded><![CDATA[<p>&#8220;You are drawing a conclusion based on an initial assumption that you are correct.&#8221; &#8212; omnicity, this sentence should apply to yourself.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: omnicity</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13742</link>
		<dc:creator>omnicity</dc:creator>
		<pubDate>Thu, 09 Feb 2006 13:49:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13742</guid>
		<description>&lt;blockquote&gt;In our discussion, the type of code executing is function code.&lt;/blockquote&gt;

That is your assertion, not mine. You are drawing a conclusion based on an initial assumption that you are correct.
It appeared to me that this was Global code, which invalidates the rest ot that portion of your argument.

&lt;blockquote&gt;Are you suggesting that Safari is the only browser getting it right? If so, read on…&lt;/blockquote&gt;
That's exactly what I was suggesting, as you are well aware.
I presume that you have seen a program similar to our &lt;code&gt;Who wants to be a Millionaire?&lt;/code&gt; It is well known that in the game, you must only use &lt;code&gt;Ask the Audience&lt;/code&gt; on the easier questions - 'collective ignorance' trumps 'collect intelligence' once things get tricky.
Alternatively, do you think that either George Bush or Tony Blair are &lt;em&gt;really&lt;/em&gt; the best people to run their respective countries? 
Three against one is hardly an over-whelming majority anyway!

&lt;blockquote&gt;…of course I don’t expect you to be convinced of this, &lt;/blockquote&gt;
And I don't expect you to convince me, I expect you to convince yourself before you raise a bug report.

I'm sure you have a valid reason for presenting your example code, but one of the reasons that I have had to think hard about this is that I have never written code in this style, and don't expect to: __proto__ etc. is there for a reason; I hope you have made that clear in your writings.

Regards, Mike</description>
		<content:encoded><![CDATA[<blockquote><p>In our discussion, the type of code executing is function code.</p></blockquote>
<p>That is your assertion, not mine. You are drawing a conclusion based on an initial assumption that you are correct.<br />
It appeared to me that this was Global code, which invalidates the rest ot that portion of your argument.</p>
<blockquote><p>Are you suggesting that Safari is the only browser getting it right? If so, read on…</p></blockquote>
<p>That&#8217;s exactly what I was suggesting, as you are well aware.<br />
I presume that you have seen a program similar to our <code>Who wants to be a Millionaire?</code> It is well known that in the game, you must only use <code>Ask the Audience</code> on the easier questions - &#8216;collective ignorance&#8217; trumps &#8216;collect intelligence&#8217; once things get tricky.<br />
Alternatively, do you think that either George Bush or Tony Blair are <em>really</em> the best people to run their respective countries?<br />
Three against one is hardly an over-whelming majority anyway!</p>
<blockquote><p>…of course I don’t expect you to be convinced of this, </p></blockquote>
<p>And I don&#8217;t expect you to convince me, I expect you to convince yourself before you raise a bug report.</p>
<p>I&#8217;m sure you have a valid reason for presenting your example code, but one of the reasons that I have had to think hard about this is that I have never written code in this style, and don&#8217;t expect to: __proto__ etc. is there for a reason; I hope you have made that clear in your writings.</p>
<p>Regards, Mike</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Yank</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13738</link>
		<dc:creator>Kevin Yank</dc:creator>
		<pubDate>Thu, 09 Feb 2006 13:06:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13738</guid>
		<description>Omnicity,

&lt;blockquote&gt;If you look at the page that is labelled as p.71 of: The ECMA-262 Spec you will see three variations on ‘Function Definition’.&lt;/blockquote&gt;

All along I've been saying that the value of &lt;code&gt;this&lt;/code&gt; depends on how the function is &lt;em&gt;called&lt;/em&gt;, not how it is declared. Quoting from &lt;a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" rel="nofollow"&gt;the spec&lt;/a&gt;...

From p.39:

&lt;blockquote&gt;10.1.7 This
There is a &lt;strong&gt;this&lt;/strong&gt; value associated with every active execution context. The &lt;strong&gt;this&lt;/strong&gt; value depends on the caller and the type of code being executed and is determined when control enters the execution context. The &lt;strong&gt;this&lt;/strong&gt; value associated with an execution context is immutable.&lt;/blockquote&gt;

Translation: &lt;code&gt;this&lt;/code&gt; depends on how and where the currently-executing code was called, and what type of code is currently executing (global code, eval code, or function code). In our discussion, the type of code executing is function code.

So how is &lt;code&gt;this&lt;/code&gt; determined for function code?

From p.40:

&lt;blockquote&gt;The caller provides the &lt;strong&gt;this&lt;/strong&gt; value. If the &lt;strong&gt;this&lt;/strong&gt; value provided by the caller is not an object (including the case where it is &lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt;), then the &lt;strong&gt;this&lt;/strong&gt; value is the global object.&lt;/blockquote&gt;

The "global object" in a browser environment is &lt;code&gt;window&lt;/code&gt;.

Translation: when the caller doesn't provide a value for &lt;code&gt;this&lt;/code&gt; (i.e. the function was called as a standalone function (e.g. &lt;code&gt;doAlert();&lt;/code&gt;), &lt;code&gt;this&lt;/code&gt; is &lt;code&gt;window&lt;/code&gt;. But when the caller &lt;em&gt;does&lt;/em&gt; provide a value (e.g. &lt;code&gt;&lt;em&gt;value&lt;/em&gt;.doAlert();&lt;/code&gt;), &lt;code&gt;this&lt;/code&gt; will be that value.

Not quite convinced? This is stated more formally later in the spec.

From p.44:

&lt;blockquote&gt;11.2.3 Function Calls
The production &lt;em&gt;CallExpression&lt;/em&gt; : &lt;em&gt;MemberExpression&lt;/em&gt; &lt;em&gt;Arguments&lt;/em&gt; is evaluated as follows:
[...]
6. If Type(Result(1)) is Reference, Result(6) is GetBase(Result(1)). Otherwise, Result(6) is &lt;strong&gt;null&lt;/strong&gt;.
7. If Result(6) is an activation object, Result(7) is &lt;strong&gt;null&lt;/strong&gt;. Otherwise, Result(7) is the same as Result(6).
8. Call the [[Call]] method on Result(3), providing Result(7) as the &lt;strong&gt;this&lt;/strong&gt; value and providing the list Result(2) as the argument values.
&lt;/blockquote&gt;

There are a lot of formal declarations to pull apart to understand this, but basically it comes down to Result(7) being &lt;code&gt;caller&lt;/code&gt; in &lt;code&gt;caller.someFunction()&lt;/code&gt; when &lt;em&gt;MemberExpression&lt;/em&gt; is of the form &lt;code&gt;caller.someFunction&lt;/code&gt;, and being &lt;code&gt;null&lt;/code&gt; when &lt;em&gt;MemberExpression&lt;/em&gt; is of the form &lt;code&gt;someFunction&lt;/code&gt;.

Back to you, omnicity:

&lt;blockquote&gt;Kevin: I know that you were able to synthesise a similar construct with individual statements that worked the way that you wanted, but it was not the same as merely splitting the compound statement in two.&lt;/blockquote&gt;

In this case, it is exactly the same. Starting with this:

&lt;pre&gt;&lt;code&gt;img.onload = function() { 
  alert(this); // What is this? 
};&lt;/code&gt;&lt;/pre&gt;

...if the &lt;em&gt;only&lt;/em&gt; change I make is to declare the function with a name rather than using an anonymous function, then this is what I get:

&lt;pre&gt;&lt;code&gt;img.onload = doAlert;

function doAlert() {
  alert(this); // What is this?
};&lt;/code&gt;&lt;/pre&gt;

The only side-effect of this change is to declare &lt;code&gt;doAlert&lt;/code&gt; as a function in the global (&lt;code&gt;window&lt;/code&gt; context) namespace. But the fact that I assign a copy of it to &lt;code&gt;onload&lt;/code&gt; (thereby putting that copy in &lt;code&gt;img&lt;/code&gt; context) means that there will be no change to the way the function executes in response to the event.

To get &lt;em&gt;your&lt;/em&gt; form, I need to make an additional change, which is to wrap the reference to the newly-declared function in another anonymous function reference:

&lt;pre&gt;&lt;code&gt;img.onload = function() { doAlert(); };

function doAlert() {
  alert(this); // What is this?
}&lt;/code&gt;&lt;/pre&gt;

With this change, &lt;code&gt;doAlert&lt;/code&gt; is no longer in &lt;code&gt;img&lt;/code&gt; context when it runs in response to the event. It's the anonymous function that has &lt;code&gt;img&lt;/code&gt; context, and it calls &lt;code&gt;doAlert&lt;/code&gt; in the global namespace, thus causing &lt;code&gt;this&lt;/code&gt; to be &lt;code&gt;window&lt;/code&gt;.

...of course I don't expect you to be convinced of this, as you seem to have a different understanding of the meaning of the syntax involved than I do anyway. Allow me to take another approach to convincing you.

Consider my code again:

&lt;pre&gt;&lt;code&gt;img.onload = function() { 
  alert(this); // What is this? 
};&lt;/code&gt;&lt;/pre&gt;

Also consider my alternative formulation:

&lt;pre&gt;&lt;code&gt;img.onload = doAlert;

function doAlert() { 
  alert(this); // What is this? 
};&lt;/code&gt;&lt;/pre&gt;

If we set this script up so that &lt;code&gt;img&lt;/code&gt; is a reference to an HTML &lt;code&gt;img&lt;/code&gt; element, and then assign &lt;code&gt;img.src&lt;/code&gt; a new value (as my original code above does), then executing either of these code formulations on a range of current browsers (Internet Explorer 6, Firefox 1, and Opera 8 among others) will reveal that &lt;code&gt;this&lt;/code&gt; is a reference to the &lt;code&gt;img&lt;/code&gt; object on &lt;em&gt;every&lt;/em&gt; browser except Safari.

Are you suggesting that Safari is the only browser getting it right? If so, read on...

Now consider this code:

&lt;pre&gt;&lt;code&gt;mylink.onclick = function() { 
  alert(this); // What is this? 
};&lt;/code&gt;&lt;/pre&gt;

Also consider the alternative formulation:

&lt;pre&gt;&lt;code&gt;mylink.onclick = doAlert;

function doAlert() { 
  alert(this); // What is this? 
};&lt;/code&gt;&lt;/pre&gt;

This time, make &lt;code&gt;mylink&lt;/code&gt; a reference to an HTML hyperlink (an &lt;code&gt;a&lt;/code&gt; element with an &lt;code&gt;href&lt;/code&gt; attribute). When you click the link in the range of current browsers (including Internet Explorer 6, Firefox 1, Opera 8, &lt;strong&gt;and Safari!&lt;/strong&gt;), either of these formulations will reveal that &lt;code&gt;this&lt;/code&gt; is the hyperlink (the value of its &lt;code&gt;href&lt;/code&gt; will be displayed, as that is what the &lt;code&gt;toString&lt;/code&gt; method of a hyperlink is supposed to do).

If Safari were right in the previous case, why is it wrong in this case? Are you saying Safari is the only browser to get it right, and it &lt;em&gt;only&lt;/em&gt; gets it right when it comes to the &lt;em&gt;onload&lt;/em&gt; event handler of an &lt;code&gt;Image&lt;/code&gt; object?

You can test even more generally by running this code on various browsers:

&lt;pre&gt;&lt;code&gt;var myObject = {};
myObject.myMethod = function() {
  alert(this);
}

myObject.myMethod(); // outputs [object Object]&lt;/code&gt;&lt;/pre&gt;

As you can see, &lt;code&gt;this&lt;/code&gt; is not &lt;code&gt;window&lt;/code&gt;, but the object that I stored in &lt;code&gt;myObject&lt;/code&gt;. This result is consistent across all current browsers, including Safari.

And my alternative formulation still holds:

&lt;pre&gt;&lt;code&gt;var myObject = {};
myObject.myMethod = doAlert;

function doAlert() {
  alert(this);
}

myObject.myMethod(); // outputs [object Object]&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Omnicity,</p>
<blockquote><p>If you look at the page that is labelled as p.71 of: The ECMA-262 Spec you will see three variations on ‘Function Definition’.</p></blockquote>
<p>All along I&#8217;ve been saying that the value of <code>this</code> depends on how the function is <em>called</em>, not how it is declared. Quoting from <a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" rel="nofollow">the spec</a>&#8230;</p>
<p>From p.39:</p>
<blockquote><p>10.1.7 This<br />
There is a <strong>this</strong> value associated with every active execution context. The <strong>this</strong> value depends on the caller and the type of code being executed and is determined when control enters the execution context. The <strong>this</strong> value associated with an execution context is immutable.</p></blockquote>
<p>Translation: <code>this</code> depends on how and where the currently-executing code was called, and what type of code is currently executing (global code, eval code, or function code). In our discussion, the type of code executing is function code.</p>
<p>So how is <code>this</code> determined for function code?</p>
<p>From p.40:</p>
<blockquote><p>The caller provides the <strong>this</strong> value. If the <strong>this</strong> value provided by the caller is not an object (including the case where it is <strong><code>null</code></strong>), then the <strong>this</strong> value is the global object.</p></blockquote>
<p>The &#8220;global object&#8221; in a browser environment is <code>window</code>.</p>
<p>Translation: when the caller doesn&#8217;t provide a value for <code>this</code> (i.e. the function was called as a standalone function (e.g. <code>doAlert();</code>), <code>this</code> is <code>window</code>. But when the caller <em>does</em> provide a value (e.g. <code><em>value</em>.doAlert();</code>), <code>this</code> will be that value.</p>
<p>Not quite convinced? This is stated more formally later in the spec.</p>
<p>From p.44:</p>
<blockquote><p>11.2.3 Function Calls<br />
The production <em>CallExpression</em> : <em>MemberExpression</em> <em>Arguments</em> is evaluated as follows:<br />
[&#8230;]<br />
6. If Type(Result(1)) is Reference, Result(6) is GetBase(Result(1)). Otherwise, Result(6) is <strong>null</strong>.<br />
7. If Result(6) is an activation object, Result(7) is <strong>null</strong>. Otherwise, Result(7) is the same as Result(6).<br />
8. Call the [[Call]] method on Result(3), providing Result(7) as the <strong>this</strong> value and providing the list Result(2) as the argument values.
</p></blockquote>
<p>There are a lot of formal declarations to pull apart to understand this, but basically it comes down to Result(7) being <code>caller</code> in <code>caller.someFunction()</code> when <em>MemberExpression</em> is of the form <code>caller.someFunction</code>, and being <code>null</code> when <em>MemberExpression</em> is of the form <code>someFunction</code>.</p>
<p>Back to you, omnicity:</p>
<blockquote><p>Kevin: I know that you were able to synthesise a similar construct with individual statements that worked the way that you wanted, but it was not the same as merely splitting the compound statement in two.</p></blockquote>
<p>In this case, it is exactly the same. Starting with this:</p>
<pre><code>img.onload = function() { 
  alert(this); // What is this? 
};</code></pre>
<p>&#8230;if the <em>only</em> change I make is to declare the function with a name rather than using an anonymous function, then this is what I get:</p>
<pre><code>img.onload = doAlert;

function doAlert() {
  alert(this); // What is this?
};</code></pre>
<p>The only side-effect of this change is to declare <code>doAlert</code> as a function in the global (<code>window</code> context) namespace. But the fact that I assign a copy of it to <code>onload</code> (thereby putting that copy in <code>img</code> context) means that there will be no change to the way the function executes in response to the event.</p>
<p>To get <em>your</em> form, I need to make an additional change, which is to wrap the reference to the newly-declared function in another anonymous function reference:</p>
<pre><code>img.onload = function() { doAlert(); };

function doAlert() {
  alert(this); // What is this?
}</code></pre>
<p>With this change, <code>doAlert</code> is no longer in <code>img</code> context when it runs in response to the event. It&#8217;s the anonymous function that has <code>img</code> context, and it calls <code>doAlert</code> in the global namespace, thus causing <code>this</code> to be <code>window</code>.</p>
<p>&#8230;of course I don&#8217;t expect you to be convinced of this, as you seem to have a different understanding of the meaning of the syntax involved than I do anyway. Allow me to take another approach to convincing you.</p>
<p>Consider my code again:</p>
<pre><code>img.onload = function() { 
  alert(this); // What is this? 
};</code></pre>
<p>Also consider my alternative formulation:</p>
<pre><code>img.onload = doAlert;

function doAlert() { 
  alert(this); // What is this? 
};</code></pre>
<p>If we set this script up so that <code>img</code> is a reference to an HTML <code>img</code> element, and then assign <code>img.src</code> a new value (as my original code above does), then executing either of these code formulations on a range of current browsers (Internet Explorer 6, Firefox 1, and Opera 8 among others) will reveal that <code>this</code> is a reference to the <code>img</code> object on <em>every</em> browser except Safari.</p>
<p>Are you suggesting that Safari is the only browser getting it right? If so, read on&#8230;</p>
<p>Now consider this code:</p>
<pre><code>mylink.onclick = function() { 
  alert(this); // What is this? 
};</code></pre>
<p>Also consider the alternative formulation:</p>
<pre><code>mylink.onclick = doAlert;

function doAlert() { 
  alert(this); // What is this? 
};</code></pre>
<p>This time, make <code>mylink</code> a reference to an HTML hyperlink (an <code>a</code> element with an <code>href</code> attribute). When you click the link in the range of current browsers (including Internet Explorer 6, Firefox 1, Opera 8, <strong>and Safari!</strong>), either of these formulations will reveal that <code>this</code> is the hyperlink (the value of its <code>href</code> will be displayed, as that is what the <code>toString</code> method of a hyperlink is supposed to do).</p>
<p>If Safari were right in the previous case, why is it wrong in this case? Are you saying Safari is the only browser to get it right, and it <em>only</em> gets it right when it comes to the <em>onload</em> event handler of an <code>Image</code> object?</p>
<p>You can test even more generally by running this code on various browsers:</p>
<pre><code>var myObject = {};
myObject.myMethod = function() {
  alert(this);
}

myObject.myMethod(); // outputs [object Object]</code></pre>
<p>As you can see, <code>this</code> is not <code>window</code>, but the object that I stored in <code>myObject</code>. This result is consistent across all current browsers, including Safari.</p>
<p>And my alternative formulation still holds:</p>
<pre><code>var myObject = {};
myObject.myMethod = doAlert;

function doAlert() {
  alert(this);
}

myObject.myMethod(); // outputs [object Object]</code></pre>]]></content:encoded>
	</item>
	<item>
		<title>By: omnicity</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13683</link>
		<dc:creator>omnicity</dc:creator>
		<pubDate>Wed, 08 Feb 2006 11:37:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13683</guid>
		<description>To get back on the original topic, I have tried to go to the horses mouth, to see what &lt;em&gt;should&lt;/em&gt; happen in this situation. 

If you look at the page that is labelled as p.71 of:
&lt;a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" rel="nofollow"&gt;The ECMA-262 Spec&lt;/a&gt;
 you will see three variations on 'Function Definition'.
The second one, (the first 'FunctionExpression'), I believe is what we are discussing here. Unfortunatly, in that document there is at least a minor typo: since step two states: &lt;code&gt;Return Result(2).&lt;/code&gt;
Which is recursive and illogical, and appears to have knocked out the formatting for the next line.

Does anyone have a correct copy of this section? There is no explanation of why this block differs from the other two, so I don't want to second-guess the authors intentions.

Personally, I believe that any compound statement in any language should act the same way as the individual statements evaluated individually. If they don't then it is a bug in the the language. Kevin: I know that you were able to synthesise a similar construct with individual statements that worked the way that you wanted, but it was not the same as merely splitting the compound statement in two.</description>
		<content:encoded><![CDATA[<p>To get back on the original topic, I have tried to go to the horses mouth, to see what <em>should</em> happen in this situation. </p>
<p>If you look at the page that is labelled as p.71 of:<br />
<a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" rel="nofollow">The ECMA-262 Spec</a><br />
 you will see three variations on &#8216;Function Definition&#8217;.<br />
The second one, (the first &#8216;FunctionExpression&#8217;), I believe is what we are discussing here. Unfortunatly, in that document there is at least a minor typo: since step two states: <code>Return Result(2).</code><br />
Which is recursive and illogical, and appears to have knocked out the formatting for the next line.</p>
<p>Does anyone have a correct copy of this section? There is no explanation of why this block differs from the other two, so I don&#8217;t want to second-guess the authors intentions.</p>
<p>Personally, I believe that any compound statement in any language should act the same way as the individual statements evaluated individually. If they don&#8217;t then it is a bug in the the language. Kevin: I know that you were able to synthesise a similar construct with individual statements that worked the way that you wanted, but it was not the same as merely splitting the compound statement in two.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Yank</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13679</link>
		<dc:creator>Kevin Yank</dc:creator>
		<pubDate>Wed, 08 Feb 2006 07:24:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13679</guid>
		<description>memet,

I see what you're trying to say, and it's basically the same thing I'm trying to say -- it's just a different way of looking at things.

You're saying: if a function is assigned as the value of an object property ("named"), then it receives the object as the value of &lt;code&gt;this&lt;/code&gt;. But if it is just called outright ("called"), it has no object as its context and therefore &lt;code&gt;this&lt;/code&gt; gets &lt;code&gt;window&lt;/code&gt; as its value.

I'm saying: when a function is called as an object method (i.e. the function has been assigned as the value of any object property), it executes with that object as its context, so &lt;code&gt;this&lt;/code&gt; is the object. But if a function is called as a simple function, then it doesn't receive any special object as its context, so &lt;code&gt;this&lt;/code&gt; is the &lt;code&gt;window&lt;/code&gt;.

We're saying the same thing, just using different terminology and a slightly different point of view.</description>
		<content:encoded><![CDATA[<p>memet,</p>
<p>I see what you&#8217;re trying to say, and it&#8217;s basically the same thing I&#8217;m trying to say &#8212; it&#8217;s just a different way of looking at things.</p>
<p>You&#8217;re saying: if a function is assigned as the value of an object property (&#8221;named&#8221;), then it receives the object as the value of <code>this</code>. But if it is just called outright (&#8221;called&#8221;), it has no object as its context and therefore <code>this</code> gets <code>window</code> as its value.</p>
<p>I&#8217;m saying: when a function is called as an object method (i.e. the function has been assigned as the value of any object property), it executes with that object as its context, so <code>this</code> is the object. But if a function is called as a simple function, then it doesn&#8217;t receive any special object as its context, so <code>this</code> is the <code>window</code>.</p>
<p>We&#8217;re saying the same thing, just using different terminology and a slightly different point of view.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: memet</title>
		<link>http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13666</link>
		<dc:creator>memet</dc:creator>
		<pubDate>Wed, 08 Feb 2006 04:08:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/#comment-13666</guid>
		<description>Kevin (and Omnicity):

 I think the bottom line that both of you have not mentionned is that the language is interpreted as statements. As such:
&lt;code&gt;
var myFunc = doSomething; //1
&lt;/code&gt;
and
&lt;code&gt;
var myFunc = doSomething(); //2
&lt;/code&gt;

 Are two completely different statements. The first one assigns a function 'pointer', or a function call 'context' (including possibly a closure) to myFunc, whereas the second one calls doSomething'2 and assigns myFunc the result of that function call.
 This might seem trivial, but it's essential: you can name a function without calling it. And by assigning by name you make a copy of the value of the variable (in this case a function).
 Anonymous functions are also a way of naming a function without giving it a name. They do not execute the function, just describe it.

 As you will notice in the examples, copying is via:
&lt;code&gt;
element.onclick = doSomething
element.addEventListener('click',doSomething,false)
element.onclick = function () {this.style.color = '#cc0000';}
&#60;element onclick="this.style.color = '#cc0000';"&#62;
&lt;/code&gt;
and refering via:
&lt;code&gt;
element.onclick = function () {doSomething()}
element.attachEvent('onclick',doSomething)
&#60;element onclick="doSomething()"&#62;
&lt;/code&gt;

 The first samples are all naming a function, whereas the last ones are all calling it. Except for the pesky attachEvent method which seems to not really abide by a standard way of doing things (namely copying the function 'pointer' instead of instancing it).

  Kevin, I personally don't using the term "method of" for these discussions. It is more like you are overriding a virtual function pointer on an object. The method is always there, it's called "onclick", you're just changing the value it holds.

 The ruling logic is not about being a method of, but rather the difference of naming a function versus calling it. That is, these things are not limited to object methods.

 In essence, it is the onclick method that has a 'this' context. Not the actual doSomething function. But when you set the value of onclick to doSomething, you have given it the same context.


 Anyways, my two cents.</description>
		<content:encoded><![CDATA[<p>Kevin (and Omnicity):</p>
<p> I think the bottom line that both of you have not mentionned is that the language is interpreted as statements. As such:<br />
<code>
var myFunc = doSomething; //1
</code><br />
and<br />
<code>
var myFunc = doSomething(); //2
</code></p>
<p> Are two completely different statements. The first one assigns a function &#8216;pointer&#8217;, or a function call &#8216;context&#8217; (including possibly a closure) to myFunc, whereas the second one calls doSomething&#8217;2 and assigns myFunc the result of that function call.<br />
 This might seem trivial, but it&#8217;s essential: you can name a function without calling it. And by assigning by name you make a copy of the value of the variable (in this case a function).<br />
 Anonymous functions are also a way of naming a function without giving it a name. They do not execute the function, just describe it.</p>
<p> As you will notice in the examples, copying is via:<br />
<code>
element.onclick = doSomething
element.addEventListener('click',doSomething,false)
element.onclick = function () {this.style.color = '#cc0000';}
&lt;element onclick="this.style.color = '#cc0000';"&gt;
</code><br />
and refering via:<br />
<code>
element.onclick = function () {doSomething()}
element.attachEvent('onclick',doSomething)
&lt;element onclick="doSomething()"&gt;
</code></p>
<p> The first samples are all naming a function, whereas the last ones are all calling it. Except for the pesky attachEvent method which seems to not really abide by a standard way of doing things (namely copying the function &#8216;pointer&#8217; instead of instancing it).</p>
<p>  Kevin, I personally don&#8217;t using the term &#8220;method of&#8221; for these discussions. It is more like you are overriding a virtual function pointer on an object. The method is always there, it&#8217;s called &#8220;onclick&#8221;, you&#8217;re just changing the value it holds.</p>
<p> The ruling logic is not about being a method of, but rather the difference of naming a function versus calling it. That is, these things are not limited to object methods.</p>
<p> In essence, it is the onclick method that has a &#8216;this&#8217; context. Not the actual doSomething function. But when you set the value of onclick to doSomething, you have given it the same context.</p>
<p> Anyways, my two cents.</p>]]></content:encoded>
	</item>
</channel>
</rss>
