糯米文學吧

位置:首頁 > 計算機 > java語言

從零碼起調用javascript

java語言9.15K

Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。以下是小編為大家搜索整理的從零碼起調用javascript,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!

從零碼起調用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;} ;

}