What about JSObject? Object.prototype.hasOwnProperty.call(obj, prop)

Art Fiedler artfiedler at gmail.com
Fri Dec 2 10:43:53 UTC 2016


Thanks for pointing out the alternative, I hadn't thought of that option.

However. ;) I would still argue this as a bug or overlooked. I understand
JSObject is not intended to be a real ScriptObject with a prototype etc.
However let me ask you the difference between hasMember and hasOwnProperty?
They appear to have the same function just different names.

On to why I think its a bug or that it was overlooked, if you ask the
internet, "javascript how to copy an object", the first thing you will find
is...

http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object

and for the last 7 year's(855,148 views later) the answer has to do
something similar to this (for objects)...
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr))
            copy[attr] = obj[attr];
    }

The static function Object.prototype.hasOwnProperty.call(obj,'property') is
however just an extension of obj.hasOwnProperty(). I wasn't expecting an
actual prototype on the JSObject. So if you take a look at some popular
javascript libraries, like lesscss, csso, uglifyjs2, typescript, etc. they
are all using obj.hasOwnProperty or Object.prototype.hasOwnProperty.call to
copy basic objects, which I believe JSObject is intended to be.

As a workaround for lesscss I just slapped a method on my class called
hasOwnProperty and that solved that issue. The others though seem to use
the static method so I hi-jacked Object.prototype.hasOwnProperty in
javascript and if its a JSObject I return hasMember otherwise call the
original function. This all seems a little too dirty though for just
copying an object.

So what I would recommend is adding a default implementation of
hasOwnProperty to call hasMember in the JSObject interface and supporting
the static calls for properties/keys on the Object.prototype, keys,
values and properties are already part of the JSObject, why not make them
more accessible to standards and get more use out of them?
Current Standards:
    Object.assign(), Object.keys(), Object.values(),
    Object.prototype.hasOwnProperty.call,
    Object.getOwnPropertyNames()

EMCAScript 2017
    Object.entries()

Thanks,
Arthur Fiedler


JSObject is a way to implement script friendly Java objects. But
> JSObjects can not be substituted in all places where a real ScriptObject
> is expected. In particular, JSObjects don't have any notion of prototype
> and so JSObjects are not on par with ECMAScript script objects.
>
> But, you can bind your POJO to an empty script object and pass it to
> ECMAScript APIs.
>
> See also:
>
> https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-Object.bindProperties
>
> HTH,
>
> -Sundar
>


More information about the nashorn-dev mailing list