Jens Thiele wrote:
... . But now I do not understand why
the following happens.
x=function(text){return function(){print(text);};}("hello world");
x(); // will print hello world as expected
foo={x:1,y:2}
foo.watch("x",x);
You have stumbled into a grey area here, the - watch - method of objects
did not make it into ECMA 262 and so does not have a public formal
specification. So exactly how it will behave where implemented is
uncertain.
My copy of the Netscape JavaScript 1.4 documentation says that -
obj.watch('propName', handler) - will call the - handler - when an
assignment is made to the property of - obj - with the specified name.
Calling it as - handler('propName' oldval, newval) and assigning the
function's return value to the property of - obj.
If the handler is an inner function then it should still have the
Activation/Variable object of its outer function's execution context in
its scope chain when it is called.
foo.x=10; // will print undefined and not hello world
i don't quite understand this
it seems in the second case the context is different
i tried the following:
x=function(text){return
function(){print("this:"+this);print("this.foomem:"+this.y);
print("text:"+text);};}("hello world");
x();
foo={x:1,foomem:2}
foo.watch("x",x);
foo.x=10;
output:
this:[object global]
So which javascript implementation are you using? I don''t recall the
toString method of a global object ever returning that particular string
(not that I look at that string value often).
this.foomem:undefined
text:hello world
That all conforms to my expetations.
But this is interesting, for the - this - keyword to refer to an object
the handler must have been called as a method of that object, and the
Netscape JavaScript 1.4 documentation does not mention that at all, the
expectation would be that - this - remained a reference to the global
object.
However, the object that is being referred to by - this - is not the
object on which the - watch - method was called, else - this.foomem -
would not be undefined.
By ECMA specification the only way that the - text - property of the
Activation/Variable object from the execution context of the outer
function, on the scope chain of the inner function, could be returning -
undefined - in the inner function is if an object higher in the scope
chain for the inner function's execution context had a defined property
named - text - that had an undefined value, masking the original -
text - parameter property.
It is not impossible for an implementation to add objects to the scope
chain of a function call, it is even common with event handling
functions generated from HTML attribute strings, but there doesn't seem
to be any reason for doing so in the context of - watch.
On the other hand, without a formal specification for - watch - it is
impossible to say what it should be doing when executing - handler -, it
could call - handler - as a method of some arbitrary object, and add any
number of unknown objects to the scope chain for the function call. And
nobody could say it was wrong for doing so.
=> the first time this is the global object the second time it is foo
It is not - foo - else - this.foomem - would return the value 2 rather
than undefined.
does it matter?
text is not in the global object
I get something wrong here
You may be wrong to have expectation of methods outside of the ECMA
specification, though the behaviour of the implementation you are using
is certainly unexpected.
There can be no help without knowing what you are trying to achieve.
Richard.