Oops, yes, I meant to reply all (darn the default reply on this list). Oh well, then yeh, I have a similar challenge right now. :) I'm going to test out something like scope.getPrototype() (that should get the prototype Scriptable object, e.g. the scope), then call the put() method of that prototype scope.
In theory (at least in my head) that might work, but I haven't tried it out just yet, I was going to play with that soon. If you know differently or try it out first let me know. Dave -----Original Message----- From: Peter Michaux [mailto:[EMAIL PROTECTED] Sent: Friday, November 14, 2008 8:03 AM To: David Parks Subject: Re: create a JavaScript String object in Java? Hi David, Thanks for the response. I have the JavaScript part about prototypes etc pretty well understood. JavaScript isn't the hard part for me. It's the Java part. :) Hopefully someone on the list can inform me how to do it in Java. If NativeArray was public then I'd be all set. BTW, you sent the response to only me. On this list you need to click "reply all". Peter On Thu, Nov 13, 2008 at 10:58 PM, David Parks <[EMAIL PROTECTED]> wrote: > Adding properties to any object is supported within javascript (assuming you > haven't sealed the object in your scope so that it can't be changed by JS). > > You can execute the JS command "myString.prototype.prop = 0", by calling the > prototype of a String object you are essentially calling the base object > from which all strings are derived, thus by adding a property to the > prototype you are also adding the property to all instances of a string > object (this happens dynamically, so you could execute this command after > you have 100 instances of a sting and this command would add the property to > all 100 instances of the string). > > Alternatively you could just add the property to a single instance of the > string by adding it to the single instance of the string (not using the > prototype) "myString.prop=0" will simply add a property to the single > instance of the string myString. > > Do a quick google lookup of "javascript prototype" if any of this isn't > clear, there are good sites with full articles and examples of this concept > out there. > > What I can't answer for you is whether it's possible to add this property to > the String object proptotype directly from java, or whether you actually > need to compile and execute a script in order to run this command and set up > the environment the way you want it (e.g. adding the property to the string > prototype). I'm dealing with that question myself right now, so if anyone > wants to extend and clarify my answer I'd love it. > > Dave > > > > -----Original Message----- > From: > [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > a.org] On Behalf Of Peter Michaux > Sent: Friday, November 14, 2008 7:15 AM > To: [email protected] > Subject: create a JavaScript String object in Java? > > How do I create a JavaScript string object in Java? > > I thought I might be able to do something like this > > ScriptableObject str = new NativeString("foo"); > > but the NativeString class is not available outside the > org.mozilla.javascript.rhino package. > > The reason I want to do this is I would then like to add some > properties to that String object like so > > str.defineProperty("prop", "val", 0); > > and then pass the str object into some JavaScript code. > > Thanks, > Peter > _______________________________________________ > dev-tech-js-engine-rhino mailing list > [email protected] > https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino > > > _______________________________________________ dev-tech-js-engine-rhino mailing list [email protected] https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
