Subj : Re: scope aliases To : netscape.public.mozilla.jseng From : Igor Bukanov Date : Sun Sep 05 2004 12:02 am Stuart wrote: > Hi, > > Earlier I asked some questions regarding scopes, now that I have worked with > them some more I have a couple more questions. > > (1) Can a scope alias be renamed? e.g. > > If initially I set the alias of myScope to "document": > > ScriptableObject.putProperty(myScope, "document", myScope); > > In some circumstances I need to temporarily alias the above scope as > "application" > > ScriptableObject.deleteProperty(myScope, "document"); > ScriptableObject.putProperty(myScope, "application", myScope); > > Will any vars defined keep their values after the 're-name'? Yes, since delete/put just affects the properties been deleted or assigned. > > --> As an after thought it would be even better if myScope could be shallow > copied so that I could have two scopes one aliased as "document" the other > as "application" (but internally they have the same information and if I > change document.x=5 then application.x will also equal 5) - Is this > possible? Yes, if you write you custom implementation of Scriptable interface with desired semantics. > > (2) Apparently in ECMAScript "document.x" is an illegal variable name (e.g. > var document.x). Is this correct? Yes. > > If I had a scope aliased as "document" and a var called x, I could use the > 'variable name' "document.x" in my script e.g. > > document.x = 5 > > Assuming this is correct. If I wanted to define the following variable > names in document scope how can it be done e.g. > > user.first (so this could be referenced as document.user.first or simply > as user.first) > user.last " > user.age " > > Based on what ECMAScript says is an illegal variable name I'm assuming I > could not use: > > ScriptableObject.defineProperty(docScope, "user.first", "Bob", 0); You need to create an object that will be accessible through "user" property, populate it with properties and then put it into the scope. You can do through Rhino API like Context.newObject() and ScriptableObject.putProperty(), but it would be much simpler if you do it from JS like in: var user = { fist: "John", last: "Smith" }; Regards, Igor .