I did a blog post about this a few months ago, with rhino, you cant just "add" to classes, but you can add to objects.
http://www.gorilla3d.com/v8/programming-java-with-javascript-via-rhino.html ----------------------------------------------------------- Extending Java Classes Objects Sadly if its a Java class and not a Javascript Object you cant add functions / prototype unless you use a JavaAdapter. Even then its not the class your extending its the Object. importPackage(java.awt); var object = JavaAdapter(java.lang.Object); // Let extend this object.value = [] object.toString = function () { return this.values.join(', ');; }; // or var object = new JavaAdapter(Color, java.lang.Object, { values: [], toString: function () { return this.values.join(', '); } }); ------------------------------------------------------------------------------- Reusing Extended Java Objects Once an object is created you cant call the constructor new on that object, instead you'll receive an error about it not a function. So you can just wrap a function around the object. The "new" is redundant and really has no effect on this functions, but it still makes it feel like an object. function JMArrayObject(arrayValues) { // the same as calling super() var object = JavaAdapter(java.lang.Object, { values: null, toString: function () { return this.values.join(', '); } }); // Do your constructor stuff here object.values = arrayValues; // return the object return object; } var ao = new JMArrayObject([0.75,0.5,0.5]); print(ao); // Output: 0.75, 0.5, 0.5 On Jun 10, 8:41 am, [email protected] wrote: > Hi there, > > I'm looking to add a method to NativeArray/NativeJavaArray (preferably > both). Is there some way of doing this? > > Currently, in JavaScript, I could do: > > var a = [ 1, 2, 3, 4, 5, 6 ]; > > I can then call the method a.concat(), a.join(), etc. I can't seem to > find a way of getting a ScriptableObject of the prototype such that I > can then call defineMethod on it. > > Is there any way to do this without having to start modifying the > Rhino source code? > > Thanks, > Stewart _______________________________________________ dev-tech-js-engine-rhino mailing list [email protected] https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
