File tree Expand file tree Collapse file tree 3 files changed +85
-0
lines changed
Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change 1+ function Product ( name ) {
2+ this . name = name ;
3+ }
4+
5+ Product . prototype . stock = 0 ;
6+ Product . prototype . price = 0 ;
7+ Product . prototype . getValueOfGoods = function ( ) {
8+ return this . stock * this . price ;
9+ } ;
10+
11+ var product = new Product ( 'Pencil' ) ;
12+ product . price = 4.99 ;
13+ product . stock = 78 ;
14+
15+ print ( 'Value of Goods: ' + product . getValueOfGoods ( ) ) ;
16+
17+
18+ var getValueOfGoods = function ( javaProduct ) {
19+ var jsProduct = new Product ( ) ;
20+ Object . bindProperties ( jsProduct , javaProduct ) ;
21+ return jsProduct . getValueOfGoods ( ) ;
22+ } ;
Original file line number Diff line number Diff line change 1+ package com .winterbe .java8 ;
2+
3+ import javax .script .Invocable ;
4+ import javax .script .ScriptEngine ;
5+ import javax .script .ScriptEngineManager ;
6+
7+ /**
8+ * Bind java objects to custom javascript objects.
9+ *
10+ * @author Benjamin Winterberg
11+ */
12+ public class Nashorn5 {
13+
14+ public static void main (String [] args ) throws Exception {
15+ ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
16+ engine .eval ("load('res/nashorn5.js')" );
17+
18+ Invocable invocable = (Invocable ) engine ;
19+
20+ Product product = new Product ();
21+ product .setName ("Rubber" );
22+ product .setPrice (1.99 );
23+ product .setStock (1037 );
24+
25+ Object result = invocable .invokeFunction ("getValueOfGoods" , product );
26+ System .out .println (result );
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package com .winterbe .java8 ;
2+
3+ /**
4+ * @author Benjamin Winterberg
5+ */
6+ public class Product {
7+ private String name ;
8+ private double price ;
9+ private int stock ;
10+
11+ public String getName () {
12+ return name ;
13+ }
14+
15+ public void setName (String name ) {
16+ this .name = name ;
17+ }
18+
19+ public double getPrice () {
20+ return price ;
21+ }
22+
23+ public void setPrice (double price ) {
24+ this .price = price ;
25+ }
26+
27+ public int getStock () {
28+ return stock ;
29+ }
30+
31+ public void setStock (int stock ) {
32+ this .stock = stock ;
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments