JavaScript Array slot/member == undefined instead of null?

Art Fiedler artfiedler at gmail.com
Wed Nov 30 19:43:44 UTC 2016


@Sundararajan,

I was previously using .getSlot(index) when extracting items from a
ScriptObjectMirror when that mirror was an array until I ran into
a undefined element and that is not equal to null as I had for my
condition... so I swapped .getSlot(i) with .get(Integer.toString(i))

Are getMember(), get(), and getSlot() suppose to all return the same null
or undefined value? Or is it expected to have
getMember() and getSlot() to return Undefined object and get() to return
null?

As a side note... If this is the intended behavior this might be a better
way to get the Undefined object instead of making a
call out of nashorn to a public static method.

Here is an example... (jdk8 1.8.0_102)

Thanks,
Arthur Fiedler


    public static void main(String[] args) throws Exception {
        NashornScriptEngineFactory factory = new
NashornScriptEngineFactory();
        ScriptEngine scriptEngine = factory.getScriptEngine();
        scriptEngine.eval("globalArray = ['line1',undefined,null];");
        ScriptObjectMirror array =
(ScriptObjectMirror)scriptEngine.get("globalArray");
        System.out.println("size(): " + array.size());
        for(int i = 0; i < array.size(); i++) {
            System.out.println("Index: " + i);
            String iStr = String.valueOf(i);
            System.out.println("    GetMember('"+iStr+"'): " +
array.getMember(iStr));
            System.out.println("    Get('"+iStr+"'): " + array.get(iStr));
            System.out.println("    GetSlot("+iStr+"): " +
array.getSlot(i));
        }
    }
    /*
    size(): 3
    Index: 0
        GetMember('0'): line1
        Get('0'): line1
        GetSlot(0): line1
    Index: 1
        GetMember('1'): undefined
        Get('1'): null
        GetSlot(1): undefined
    Index: 2
        GetMember('2'): null
        Get('2'): null
        GetSlot(2): null
    */


More information about the nashorn-dev mailing list