Subj : Re: variables and scopes To : Stuart From : Igor Bukanov Date : Thu Sep 23 2004 09:19 pm Stuart wrote: > Igor, > > Thanks for your reply but I am confused! One of the e-mails you sent me > stated: > > >>>var x; >>>x = y; >>>z = y; >>>function f() { } >>> >>>Then the runtime evaluates "z = y". First it tries to see which scope >>>contains z since z is not declared as a variable. There are two cases. >>>In the first case there is property z in the scope or its parent scopes. >>>In this case the head of prototype chain containg y is be updated with >>>the value of y. <----- this sentence >>> >>>In the second case when z is not found, it is be created in the topmost >>>parent scope of the current scope and filled with the value of y. > > > Now for the rootCounter would JS not find the property in the application > scope and update it there? (it is obviously finding it otherwise the value > would not be 9). Why would it create another rootCounter if there already > is one in a higher scope? The prototype chain and scope chain are 2 independent entities where the scope chain affects name resolution while the prototype chains affects details of property get/set operations. When Rhino evaluates name = something it first binds name, that is it looks for the current scope chain and locate the scope if any that has property names "name". Now the scope has a property if the property is present in the scope object itself or anywhere on its prototype chain. Then Rhino evaluates something and sets the property "name" of the bound scope to the resulting value. In your case there is only one object on the scope chain, which is the anonymous scope. It has the property named "rootCounter" since it present in its prototype object, document scope. Thus "rootCounter" is bound to anonymous scope and after evaluating "something" Rhino effectively performs .rootCounter = which set the property in following normal JS rules about property assignments. Regards, Igor .