Implementing JavaScript index operator in a Java object

Tim Fox timvolpe at
Sat Mar 1 01:27:44 PST 2014

Hello Nashorn folks,

I have a JavaScript object, and I'd like to 'override' what the index 
operator [] does on it.

I.e. when I do

var x = myobj[3];

I actually want it to call some other function on the object, e.g.


I'm pretty sure this isn't possible in pure JS, so I was thinking of 
wrapping a Java Object as a JavaScript object, e.g. if I have

public class MyJavaClass {
    public Object get(int index) {
       return something;

I would like to have a JS wrapper for it, such that when I call:

var x = myJavaWrapper[3];

It actually calls:


Is this possible in Nashorn?

More information about the nashorn-dev mailing list