Comments on: Capture this! http://www.weaselhat.com/2007/03/14/js-this/ Sun, 29 Apr 2007 03:15:24 +0000 hourly 1 https://wordpress.org/?v=6.3.2 By: Michael Greenberg http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-74 Sun, 29 Apr 2007 03:15:24 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-74 Ah. That’s a relief that it’s a bug. I think the behavior itself is weird, and that dotting should either always or never capture this.

Thanks for looking back into this!

]]>
By: Dave Herman http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-73 Sat, 28 Apr 2007 20:38:13 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-73 Okay, it turns out I was wrong and this has nothing to do with the new behavior of `this’ in ES4 — Mozilla has not implemented that yet.

Brendan Eich informs me that it’s a bug that’s been fixed in alpha versions of Firefox/SpiderMonkey:

“[this example] changed behavior from Firefox 2 (the mozilla 1.8 branch) to Minefield (alpha Firefox 3, the 1.9 cvs trunk). My trunk js shell gives false, my 1.8 branch shell gives true in agreement with Firefox 2.0.0.x.

This looks like a bug fix due to Igor’s work in https://bugzilla.mozilla.org/show_bug.cgi?id=363530.”

He’s referring to `c.bar()()’ returning true or false, BTW. It should return `false’.

]]>
By: Dave Herman http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-72 Fri, 27 Apr 2007 18:50:29 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-72 I think I found it. Look at

http://developer.mozilla.org/es4/proposals/bug_fixes.html

and search for “[THIS.PROPAGATES]”.

]]>
By: Dave Herman http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-71 Fri, 27 Apr 2007 17:42:40 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-71 D’oh! I missed the part where they disagree: `foo.bar()()’. Sorry I posted too soon before I read more carefully.

I still need to double-check where this divergence in behavior comes from.

]]>
By: Dave Herman http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-70 Fri, 27 Apr 2007 17:40:28 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-70 I’m looking at this again and I’m confused now. What is the problem with this example? All three major browsers agree on the behavior.

The binding of `this’ in JS is determined by the syntax of the function call. When you said `c.bar()’ you invoked `bar’ as a method of `c’, so it bound `this’ to `c’ for the evaluation of the body of `bar’. But when you then called `foo’ as a naked function, it bound `this’ to the global object.

That’s the stated, specified behavior of JavaScript. Are you claiming there’s an inconsistency between browsers/implementations of JS, or just that the specified behavior is weird? (If it’s the latter, you’ll get no disagreement from me.)

]]>
By: Dan Stowell http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-61 Wed, 18 Apr 2007 17:52:16 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-61 The OL compiler doesn’t touch that bit of script, so the results I gave in the first comment are how ActionScript behaves. So c.bar() returns a function closed over a different `this`?

]]>
By: Dan Stowell http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-59 Wed, 18 Apr 2007 11:31:26 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-59 I compiled your test code to ActionScript with the OpenLaszlo compiler – the results are actually from Flash 8, though I’ll have to track down the code our compiler emits to see whose fault it is.

]]>
By: Michael Greenberg http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-57 Tue, 17 Apr 2007 15:36:48 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-57 Are you sure you’re running in the latest Firefox? Dave Herman said that it’s an EC4 feature; it might only be in the latest versions of Spidermonkey.

]]>
By: Dan Stowell http://www.weaselhat.com/2007/03/14/js-this/comment-page-1/#comment-56 Tue, 17 Apr 2007 14:50:08 +0000 http://www.weaselhat.com/2007/03/14/js-this/#comment-56 OpenLaszlo gets this wronger (but perhaps more consistently):

c.foo() true
c.bar()() false
foo() false

]]>