Search code examples
nashorn

Nashorn : are nashorn's js objects extensible


I've an issue with nashorn where i want to extend the JFrame instance.

The following code

print(Object.isExtensible(frame));

Where frame is the instance. Well this line throws a TypeError, while :

Object.prototype.toString.call(frame)

returns [Object javax.swing.JFrame]

meaning it is an object whose constructor is javax.swing.JFrame. I'm confused and would like some suggestion.


Solution

  • Java objects by themselves are not extensible (JavaScript objects are.)

    You have two options:

    1. Wrap the Java object in a JavaScript object or
    2. Use Java.extend to create a subclass. http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html