欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

java語(yǔ)言

從零碼起調(diào)用javascript

時(shí)間:2025-05-11 22:48:00 java語(yǔ)言 我要投稿
  • 相關(guān)推薦

從零碼起調(diào)用javascript

  Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。以下是小編為大家搜索整理的從零碼起調(diào)用javascript,希望能給大家?guī)?lái)幫助!更多精彩內(nèi)容請(qǐng)及時(shí)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  package co.test;

  import java.io.FileReader;

  import java.io.LineNumberReader;

  import org.mozilla.javascript.Context;

  import org.mozilla.javascript.Function;

  import org.mozilla.javascript.Scriptable;

  public class JSExploration

  {

  private Context cx;

  private Scriptable scope;

  public JSExploration()

  {

  this.cx = Context.enter();

  this.scope = cx.initStandardObjects();

  }

  public Object runJavaScript(String filename)

  {

  String jsContent = this.getJsContent(filename);

  Object result = cx.evaluateString(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 = reader.readLine()) != null)

  {

  sb.append(s).append("\n");

  }

  return sb.toString();

  }

  catch (Exception e)

  {

  // TODO Auto-generated catch block

  e.printStackTrace();

  return null;

  }

  }

  public Scriptable getScope()

  {

  return scope;

  }

  public static void main(String[] args)

  {

  String filename = System.getProperty("user.dir") + "/jsmap.js";

  JSExploration jsExploration = new JSExploration();

  Object result = jsExploration.runJavaScript(filename);

  Scriptable scope = jsExploration.getScope();

  Function sum = (Function) scope.get("sum", scope);

  Function isPrime = (Function)sum.call(Context.getCurrentContext(), scope, sum, new Object[] {2,8});

  Object ss = isPrime.call(Context.getCurrentContext(), sum, isPrime, new Object[] {2,8});

  System.out.println(Context.toString(ss));

  }

  }

  試驗(yàn)了一個(gè)java 調(diào)用 javascript 的例子,如果把jsmap.js中的與this 有關(guān)的代碼注銷(xiāo)的話程序就可以正常運(yùn)行。不住銷(xiāo)掉的話就會(huì)報(bào)個(gè)運(yùn)行時(shí)錯(cuò)誤。。。

  js 代碼如下(有關(guān)this 的代碼已注銷(xiāo)):

  function sum(x, y) {

  // this.formulaeObject = null;

  // this.formulaeObject["vager"] = function (c, d) {

  // return (c + d)/2;

  // };

  var vager = 1000;

  return function (x,y){return x + y + vager;} ;

  }

【從零碼起調(diào)用javascript】相關(guān)文章:

java調(diào)用的方法09-04

PHP調(diào)用的C代碼08-05

java調(diào)用的幾個(gè)方法07-27

系統(tǒng)調(diào)用的概念簡(jiǎn)介07-06

如何調(diào)用其他PPT07-17

JavaScript的應(yīng)用10-19

匯編調(diào)用C函數(shù)10-29

java構(gòu)造函數(shù)調(diào)用技巧10-26

C語(yǔ)言是如何調(diào)用硬件的10-01