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
chain.
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).
HTH
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann