Subj : Re: RHino newbie: getter not working To : netscape.public.mozilla.jseng From : Brian J. Sayatovic Date : Tue Jan 18 2005 08:02 pm Igor Bukanov wrote: > 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 > _______________________________________________ > mozilla-jseng mailing list > mozilla-jseng@mozilla.org > http://mail.mozilla.org/listinfo/mozilla-jseng > Actually, after all of my care to make sure I typed all of the details in, I neglected to mention that I'm already doing a defineClass for my class (although my getClassName() returns the fully quilified class name, org.trinition.mytest.MyCanvas). In fact, I think that's why the toString() of the result for "canvas;" was org.trinition.mytest.MyCanvas@13ad085 instead of undefined. It seems it is only the properties of MyCanvas that result in undefined being returned. I also tried a "for(var prop in canvas) { ...", but the loop terminates with no iterations meaning 'canvas' has no properties. I'm really baffled. I feel like I followed the documentaiton precisely, yet it doesn't work. Regards, Brian. .