Reputation: 15305
I am trying to work out how to distinguish between a javascript object and a java object inside a script running on Nashorn.
I ended up writing something like this:
function isJavaObject(oj) {
return oj.getClass && oj.hashCode
}
Is there a better way ?
Note that using instanceof against java.lang.Object does not work
oj = {}
oj instanceof Java.type("java.lang.Object") // returns true
Upvotes: 1
Views: 477
Reputation: 4595
Nashorn has a non-ECMA-standard built-in object "Java" (capital "J") that has a lot of goodies, among them the Java.isJavaObject(obj)
function that returns true if the specified object is a Java object but not a script object. There is also Java.isScriptObject(obj)
that returns almost the exact opposite, except for null
for which both functions return false
.
Upvotes: 6