Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Closure scope confusion

Thread Tools

Closure scope confusion

David Mark
Posts: n/a
Thomas Allen wrote:
> On Mar 19, 4:11 pm, John G Harris <(E-Mail Removed)> wrote:
>> That web page is seriously misleading.

> Agreed, what's the recommended resource for this question? QuirksMode
> does have some decent stuff so I thought it'd be OK.

You've got to be careful with that site. It is very much a part of the
cult of observational programming (sort of a shrine actually). In other
words, they see things in browsers and then try to guess what they mean.
Often these faulty inferences are contradicted later, leading to
complaints about "buggy browsers".
Reply With Quote
Thomas 'PointedEars' Lahn
Posts: n/a
Thomas Allen wrote:

> codethief wrote:
>> Why does a simply 'load_html(data)' (without 'this.' in front of it)
>> even work?

> Because the following makes the load_html function available in the
> global scope:
>> # window = WindowInteraction();

> "window" being the host object.

I am afraid you are seriously mistaken.

First of all, _a_ host object. Secondly, `window' would not be an object,
it would be the name of a property storing a reference to an object.
Thirdly, if `window' would resolve to the host-defined property of the
Global Object that refers to a Window instance, it is very likely that this
property would be read-only and the assignment had either no effect or
would cause a runtime error. Fourthly: If the assignment was successful,
the stored reference value would be replaced, and the property in question
would not necessarily refer to a host object anymore; it would store
whatever WindowInteraction() returned. Those are a great many ifs.

Another possible explanation that complies better with Occam's Razor is
that `window' does _not_ refer to a host object here, and that the value of
a variable or user-defined property has been overwritten.

What happens here: WindowInteraction() is called with its `this' value
being set to the Global Object (as evaluation of `WindowInteraction'
results in Reference(base=undefined, name=WindowInteraction, ...) because
the only production `MemberExpression : Identifier' can be applied), and it
contains the following statement:

this.load_html = function(html) {
// ...

As a result, the Global Object is augmented with a property named
`load_html' which stores a reference to a function object. So load_html()
can be called later without `this.' being part of the expression, either
because the Global Object is in the scope chain of any execution context,
or, less likely, because there is another callable `load_html' in the scope

After the call, an assignment to `window' is tried. Since no other object
in the scope chain has a property with that name, an attempt takes place to
overwrite the `window' property of the Global Object. This attempt might
even be successful. However, tests with both Iceweasel 3.5.8 (Gecko
1.9.1. and IE/MSHTML 6.0.2800.1106 suggest that this assignment is likely
to fail: Gecko issues a warning saying "window is read-only" without
changing the value, and MSHTML throws ReferenceError("Illegal assignment").

As is obvious now, this call is a rather bad move. However, the OP has
found out by now that the `new' was missing, which changes everything:
`this' would then no longer refer to the Global Object, but to the object
that is about to be constructed. As a result, the unqualified call
`load_html()' would fail then (except if there was another callable
load_html in the scope chain).


realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Reply With Quote
Thomas 'PointedEars' Lahn
Posts: n/a
codethief wrote:

> I just realized that the 'new' operator is missing in front of
> 'WindowInteraction()'. (However, it definitely worked without 'new' in
> the way I described above.)

As I explained.

> As 'load_html' (without 'this') didn't work, anymore, I figured out
> that 'this' may in fact be a special keyword:

It is, but differently than you think.

> # var wi = this;
> #
> # jQuery.ajax({
> # 'url': url,
> # 'data': data,
> # 'dataType': 'html',
> # 'success': function(data) {
> # wi.load_html(data);
> # }
> # });
> ... works. So, is this is the only (and your recommended) solution?

No, you can use a more direct closure:

'url': url,
'data': data,
'dataType': 'html',
'success': (function (x) {
return function() {

As for recommended: Avoid jQuery to begin with.

Secondly, you need to understand that value of `this' is determined *at*
*runtime*; it is determined by the way the method is called, not by the way
it is defined.

Next time, do not prepend code lines with anything but white-space. Posted
code should be executable as-is.

Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Reply With Quote
Thomas 'PointedEars' Lahn
Posts: n/a
codethief wrote:

> Now, that I have read the link you posted, Thomas, I think I
> understand the whole issue, now.

I seriously doubt it, given that I find the referred explanation strewn with
gaping defects in correctness.

> Thanks to both of you!

Both of who?

var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Reply With Quote
Thomas Allen
Posts: n/a
On Mar 19, 6:04*pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
> Both of who?

The only two respondents at that point, presumably.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is a closure's scope accessible by untrusted code? Andrey Fedorov Javascript 2 10-27-2008 05:29 PM
Another dumb scope question for a closure. Steven W. Orr Python 2 01-09-2008 10:58 PM
How does ruby handle scope and closure in this case? grocery_stocker Ruby 5 08-31-2007 10:54 AM
Scope (closure?) question Kidogg Javascript 2 05-27-2007 02:54 PM
Perl hangs when returning lvalue closure from another lvalue closure Julian Mehnle Perl Misc 0 07-17-2003 03:13 PM