Subj : Re: RHino newbie: getter not working To : Brian J. Sayatovic From : Igor Bukanov Date : Tue Jan 18 2005 12:08 pm Brian J. Sayatovic wrote: > I wrote a simple Java class hierarchy... > > ScriptableObject-->MyObject-->MyNode-->MyView-->MyCanvas > > The final class has this in it: > > public String jsGet_build() { > return "2"; > } > > I then have a script that looks like this: > > var canvas = new Packages.org.trinition.mytest.MyCanvas(); > canvas; // or canvas.build; > > When I execute this script, I use the following: > > String filename2 = "Test Data/test1.js"; > FileReader reader = new FileReader(filename2); > Object result = context.evaluateReader(scope, > reader, filename2, 1, null); > System.out.println("Result: " + result); > > The return value for "canvas;" is: > > Result: org.trinition.mytest.MyCanvas@13ad085 > > However, when I use "canvas.build;", I get: > > Result: org.mozilla.javascript.Undefined@18e2b22 > > I don't understand why my getter isn't working. What am I doing wrong? To make getters in classes extending from ScriptableObject to work you have to call ScriptableObject.defineClass(scope, yourClass). It will prepare your class instances to behave as standard ECMAScript objects. For example, assuming that MyCanvas contains: public String getClassName() { return "MyCanvas"; } after ScriptableObject.defineClass(scope, MyCanvas.class) the scope would contain MyCanvas constructor that you can use to create instances of MyCanvas. As with the standard objects like Date or Array you will get MyCanvas.prototype which scripts can populate with additional functions like with any other Constructor.prototype cases. MyCanvas.prototype also contains objects wrapping getters and setters and that is why without calling defineClass they do not work. Regards, Igor .