Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6168ec

Browse files
committed
pass java objects to javascript functions
1 parent28983b4 commita6168ec

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

‎res/nashorn1.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
varfun=function(name){
1+
varfun1=function(name){
22
print('Hi there from Javascript, '+name);
33
return"greetings from javascript";
4+
};
5+
6+
varfun2=function(object){
7+
print("JS Class Definition: "+Object.prototype.toString.call(object));
48
};

‎src/com/winterbe/java8/Nashorn1.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
importjavax.script.ScriptEngine;
55
importjavax.script.ScriptEngineManager;
66
importjava.io.FileReader;
7+
importjava.time.LocalDateTime;
8+
importjava.util.Date;
79

810
/**
911
* Calling javascript functions from java with nashorn.
@@ -17,8 +19,12 @@ public static void main(String[] args) throws Exception {
1719
engine.eval(newFileReader("res/nashorn1.js"));
1820

1921
Invocableinvocable = (Invocable)engine;
20-
Objectresult =invocable.invokeFunction("fun","Peter Parker");
22+
Objectresult =invocable.invokeFunction("fun1","Peter Parker");
2123
System.out.println(result);
24+
25+
invocable.invokeFunction("fun2",newDate());
26+
invocable.invokeFunction("fun2",LocalDateTime.now());
27+
invocable.invokeFunction("fun2",newPerson());
2228
}
2329

2430
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp