()方法我對Object.bindProperties()方法中的犀牛兩個問題:Object.bindProperties犀牛
- 是否有此方法的全面的文件,除了在https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions#Nashornextensions-loadWithNewGlobal?
- 它是一個如下所示的錯誤?
以下代碼未將obj2的x,y和z屬性綁定到obj,因爲obj已具有相同名稱的屬性。它是否記錄在某處?
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
--------------------------------
現在,讓我們添加一個新的屬性OBJ2。新的屬性被綁定到OBJ,但OBJ讀取新屬性的值作爲空:
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
--------------------------------
而且,這裏是工作的代碼的另一種變體。這一次,我開始將目標對象設爲空。現在,將新屬性添加到源可以正常工作。
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
----------------------------
我使用JDK版本1.8.0_20。
感謝 Kishori