国产视频最新网址|这里只有精品中文字幕情侣酒店|精品成人一区二区|91久久蜜桃网站|AV成人电影五区|欧美精品在线一区|秋霞午夜一区日韩|国产午夜成人免费看片不卡|98精品美女视频日韩一区精品视频|亚洲综合久久国产不卡日本岛国久久

調(diào)用javascript

時間:2024-08-02 06:04:56 JavaScript 我要投稿
  • 相關推薦

調(diào)用javascript

  【提要】本篇《從零碼起調(diào)用javascript》特別為需要編程學習的朋友收集整理的,僅供參考。內(nèi)容如下:

  Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進行復雜的編程。以下是小編為大家搜索整理的從零碼起調(diào)用javascript,希望能給大家?guī)韼椭?更多精彩內(nèi)容請及時關注我們考試網(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(" ");

  }

  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));

  }

  }

  試驗了一個java 調(diào)用 javascript 的例子,如果把jsmap.js中的與this 有關的代碼注銷的話程序就可以正常運行。不住銷掉的話就會報個運行時錯誤。。。

  js 代碼如下(有關this 的代碼已注銷):

  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】相關文章:

對javascript的理解08-08

java調(diào)用cmd命令01-29

常用的JavaScript模式09-22

Javascript的this用法簡述08-15

Windows網(wǎng)絡診斷怎么調(diào)用10-02

JavaScript學習筆記08-24

JavaScript 基礎教學09-29

JavaScript的課堂講解09-03

JavaScript常用方法匯總10-25

java程序中如何調(diào)用linux命令08-27