It would help a lot if you format your code:
> unsafeWindow.helloworld = function() {
>
> alert('Hello world!');
>
> // fails with error saying it does not exist
> helloworld2();
> }
>
> unsafeWindow.helloworld2 = function() {
>
> alert('Hello world!2');
>
> //fails with error saying it does not exist
> helloworld();
>
> }
You have in infinitely recursive funciton - helloworld calls helloworld2 which calls helloworld and so ad infinitum.
But anyway, you are setting a property of unsafeWindow
:
unsafeWindow.helloworld = function() {
but then attempting to call it using an unqualified identifier that is resolved on the scope chain:
[... later, in helloword2 ...]
helloworld();
So the identifier helloworld
is resolved on the scope chain of the execution/variable object created when unsafeWindow.helloworld2
is called.
So helloworld
is set as a property of unsafeWindow
. When the function is called, the identifier helloworld2
will be resolved using the scope chain of the function.
In browsers, the window/global object is on the scope chain so variable names may be found there (i.e. variables may resolve to properties of the global object if not found sooner in the scope chain), but I suspect that when unsafeWindow.helloworld
is called, that its scope chain ends with the document's global object, not unsafeWindow
. Otherwise calls to functions in the document would also have unsafeWindow
on their scope chain, which doesn't seem right to me.
Or I might be completely wrong about that. :-)