Inconsistent missing property handling with java objects

Bullotta, Rick rick.bullotta at thingworx.com
Wed Sep 3 16:05:09 UTC 2014


Ah, got it.


From: Mike Rettig [mailto:mike.rettig at gmail.com]
Sent: Wednesday, September 03, 2014 11:56 AM
To: Bullotta, Rick
Cc: Hannes Wallnoefer; nashorn-dev at openjdk.java.net
Subject: Re: Inconsistent missing property handling with java objects

How about this example which demonstrates the inconsistent behavior of js objects versus java objects?

        ScriptEngineManager engineManager =
                new ScriptEngineManager();
        final ScriptEngine nashorn = engineManager.getEngineByName("nashorn");
        nashorn.eval("'use sctrict'");
        nashorn.eval("var jsD={}");
        nashorn.eval("jsD.badProp=1233");
        nashorn.eval("print(jsD.badProp)");

        nashorn.eval("var d= new java.util.Date()");
        nashorn.eval("d.badProp=4567");
        nashorn.eval("print(d.badProp)");


This prints:

1233
undefined

For the java object, the assignment is allowed to the invalid field, but retrieving that value returns 'undefined'.  This is inconsistent with javascript objects AND java objects.

Is the intended behavior for nashorn to treat java objects like js objects or like POJO's?

Mike


More information about the nashorn-dev mailing list