Behavior of Object.bindProperties() Method of Nashorn

Kishori Sharan kishori.sharan at gmail.com
Fri Oct 10 17:38:23 UTC 2014


Hello Everyone,

I was experimenting with the Object.bindProperties() method in Nashorn and
found a few weird behaviors. I would like to ask two questions:
1. Is there a comprehensive documentation for this method, except at
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-loadWithNewGlobal
2. Is it a bug as explained in the following snippet of code?

The following code does not bind the x, y, and z properties of obj2 to obj
because obj already has properties with the same names. Is it documented
somewhere?

var obj = {x:10, y:20, z:30};

var obj2 = {x:100, y:200, z:300};



// bind properties of 'obj2' to 'obj'

Object.bindProperties(obj, obj2);



print(obj.x, obj.y, obj.z);

print(obj2.x, obj2.y, obj2.z);

---------------------------------

10 20 30

100 200 300

--------------------------------

Now, let us add a new property to obj2. The new property is bound to obj,
but obj reads the new property's value as null:

var obj = {x:10, y:20, z:30};

var obj2 = {x:100, y:200, z:300};



// Add a new property to obj2

obj2.u = 600;



// bind properties of 'obj2' to 'obj'

Object.bindProperties(obj, obj2);



print(obj.x, obj.y, obj.z, obj.u);  // obj.u is null. Why?

print(obj2.x, obj2.y, obj2.z, obj2.u);



---------------------------------

10 20 30 null

100 200 300 600

--------------------------------

And, here is another variant of the code that works. This time, I started
the target object as empty. Now, adding the new property to the source
works fine.

var obj = {};

var obj2 = {x:100, y:200, z:300};



// Add a new property to obj2

obj2.u = 600;



// bind properties of 'obj2' to 'obj'

Object.bindProperties(obj, obj2);



print(obj.x, obj.y, obj.z, obj.u);  // obj.u is correct.It is 600

print(obj2.x, obj2.y, obj2.z, obj2.u);



----------------------------

100 200 300 600

100 200 300 600

----------------------------

I am using JDK version 1.8.0_20.
Thanks
Kishori


More information about the nashorn-dev mailing list