Subj : Re: how to declare global functions in Rhino To : mttaylor From : Igor Bukanov Date : Mon Jun 28 2004 10:25 pm mttaylor wrote: > Me again, realized I didn't give enough info; I'm > using rhino. I'm passing a class to my script and in > the script I'd like to write: > > doThisFunc(); > > instead of: > > myclass.doThisFunc(); > There are few ways to get that. First, you can have a script that define such aliases, like in : function doThisFunc() { return myclass.doThisFunc(); } and then execute the script after you call Context.initStandardObjects() and before execution of other scripts. This is the easiest way. Second, you can put myclass to the prototype chain of scope. In Java it will look like: Scriptable scope = Context.initStandardObjects(); .... Scriptable myclassObj = ... myclassObj.setPrototype(scope.getPrototype()); scope.setPrototype(myclassObj); where myclassObj is the object that is exposed to JS in your example as myclass. You can also code that in pure JS using __proto__ special property and the fact that "this" keyword in top level scripts points to the scope: myclass.__proto__ = this.__proto__; this.__proto__ = myclass; After that JS runtime will look in myclass as well when resolving top-level properties. Finally, you can create an instance of org.mozilla.javascript.FunctionObject directly and then define it as top-level property in your scope: Scriptable scope = Context.initStandardObjects(); .... FunctionObject fo = new FunctionObject( "doThisFunc", instanceOfMethodReflectingJavaCode, scope); ScriptableObject.defineProperty( scope, fo.getFunctionName(), fo, ScriptableObject.DONTENUM); Regards, Igor Any help would be > greatly appreciated! > > Mark > > --- me three wrote: > >>I know this has been asked before because I've seen >>it, but unfortunately I didn't save the thread. And >>I >>don't know what to search for in the mailing list >>archive so I'll ask again, please forgive me: >> >>How do I 'hide' my class when I call a method? ie: >> >>myclassinstance.mymethod(arg1, arg2) >> >>to: >> >>mymethod(arg1, arg2) >> >>Thank you! >> >>_______________________________________________ >>mozilla-jseng mailing list >>mozilla-jseng@mozilla.org >>http://mail.mozilla.org/listinfo/mozilla-jseng > > .