BIND PROPERTIES of objects - in Nashorn Javascript (js) Engine in Java 8




Program/ Example of Nashorn Javascript (js) Engine in Java 8 - BIND PROPERTIES of objects in javascript >

package nashornJs;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
*
* BIND PROPERTIES of objects in javascript - And use it in java
*
*/
public class MyNashornClass {
   public static void main(String[] args) throws Exception {
      System.out.println("In java - START");
      
      ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
       engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");    
       System.out.println("In java - FINISH");
      
   }
}
/* OUTPUT
In java - START
In myNashornJavaScript.js
1) BIND PROPERTIES of two objects in one in javascript
2) BIND PROPERTIES of addnashornJss in employee using bindProperties in javascript
3) Display
Display - city of addnashornJss - using addnashornJss = Delhi
You can also display - city of addnashornJss - using employee = Delhi
4) Modify
5) Display after modify
Display after modify - city of addnashornJss - using addnashornJss = Paris
You can also display - city of addnashornJss - using employee = Paris
End myNashornJavaScript.js
In java - FINISH

*/



E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
// 1) BIND PROPERTIES of two objects in one in javascript
print(' 1) BIND PROPERTIES of two objects in one in javascript');
var employee = {};
var addnashornJss = { city : 'Delhi'};
// 2) BIND PROPERTIES of addnashornJss in employee using bindProperties in javascript
print('2) BIND PROPERTIES of addnashornJss in employee using bindProperties in javascript');
Object.bindProperties(employee, addnashornJss);
// 3) Display
print('3) Display');
print("Display - city of addnashornJss - using addnashornJss = " + addnashornJss.city);
print("You can also display - city of addnashornJss - using employee = " + employee.city);
// 4) Modify
//You can also change - city of addnashornJss - using employee
print('4) Modify');
employee.city = 'Paris';
//5) Display after modify
print('5) Display after modify');
print("Display after modify - city of addnashornJss - using addnashornJss = " + addnashornJss.city);
print("You can also display - city of addnashornJss - using employee = " + employee.city);
print('End myNashornJavaScript.js');




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

eEdit
Must read for you :