Richard Cornford wrote:
> 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.
it should get into ECMA - it is really useful
> 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.
yes this was my problem
with the example below I tried to understand why it does not work.
I am using spidermonkey - and this was a bug which is now fixed
(thanks to brendan)
see also:
<>
and
http://bugzilla.mozilla.org/show_bug.cgi?id=240577
>>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.foome m:"+this.y);
i had a bug here ^
this should be this.fooomem and not this.y
sorry for this one
> 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).
I am using spidermonkey and embedding it.
>>this.foomem:undefined
>>text:hello world
>
>
> That all conforms to my expetations.
yep to mine, too
>>this:[object Object]
>
>
> 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.
>
>
>>this.foomem:undefined
>
>
> 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.
this was my mistake see above (i do print this.y instead of this.foomem)
- sorry again
>>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.
okay with the patch to spidermonkey, everthing works like expected
for the record (the script without my bug and the output of a fixed
spidermonkey version):
// first test
print ("First test output");
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);
foo.x=10; // now will print hello world
// second test
print ("Second test output");
x=function(text){
return function(){
print("this:"+this);
print("this.foomem:"+this.foomem);
print("text:"+text);
};
}("hello world");
x();
foo={x:1,foomem:2}
foo.watch("x",x);
foo.x=10;
First test output
hello world
hello world
Second test output
this:[object global]
this.foomem:undefined
text:hello world
this:[object Object]
this.foomem:2
text:hello world
thanks
i am really happy now
(it works, and more important to me, my expectations were reasonable,
I thought that I still did not understand some basic language concept)