Java語言作為靜態面向物件程式語言的代表,極好地實現了面向物件理論,允許程式設計師以優雅的思維方式進行復雜的程式設計。以下是小編為大家搜尋整理的從零碼起呼叫javascript,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!
package ;
import Reader;
import NumberReader;
import ext;
import tion;
import ptable;
public class JSExploration
{
private Context cx;
private Scriptable scope;
public JSExploration()
{
= r();
e = StandardObjects();
}
public Object runJavaScript(String filename)
{
String jsContent = sContent(filename);
Object result = uateString(scope, jsContent, filename, 1, null);
return result;
}
private String getJsContent(String filename)
{
LineNumberReader reader;
try
{
reader = new LineNumberReader(new FileReader(filename));
String s = null;
StringBuffer sb = new StringBuffer();
while ((s = Line()) != null)
{
nd(s)nd("n");
}
return ring();
}
catch (Exception e)
{
// TODO Auto-generated catch block
tStackTrace();
return null;
}
}
public Scriptable getScope()
{
return scope;
}
public static void main(String[] args)
{
String filename = roperty("") + "/";
JSExploration jsExploration = new JSExploration();
Object result = avaScript(filename);
Scriptable scope = cope();
Function sum = (Function) ("sum", scope);
Function isPrime = (Function)(urrentContext(), scope, sum, new Object[] {2,8});
Object ss = (urrentContext(), sum, isPrime, new Object[] {2,8});
tln(ring(ss));
}
}
試驗了一個java 呼叫 javascript 的例子,如果把中的.與this 有關的程式碼登出的話程式就可以正常執行。不住銷掉的話就會報個執行時錯誤。。。
js 程式碼如下(有關this 的程式碼已登出):
function sum(x, y) {
// ulaeObject = null;
// ulaeObject["vager"] = function (c, d) {
// return (c + d)/2;
// };
var vager = 1000;
return function (x,y){return x + y + vager;} ;
}