And I expanded wiki page section on bindProperties after your question :-)

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-Object.bindProperties

Thanks
-Sundar

On Thursday 16 October 2014 09:25 PM, A. Sundararajan wrote:
Object.bindProperties considers properties at the time of bindings only. No "tracking" of object after it is bound. Also, only properties that don't exist in target object are bound from source (again check is at the time of binding).

Hope this helps,
-Sundar

On Friday 10 October 2014 11:08 PM, Kishori Sharan wrote:
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


Reply via email to