<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. java語言

    從零碼起調用javascript

    時間:2025-05-11 22:48:00 java語言 我要投稿
    • 相關推薦

    從零碼起調用javascript

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

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

      }

      }

      試驗了一個java 調用 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;} ;

      }

    【從零碼起調用javascript】相關文章:

    java調用的方法09-04

    PHP調用的C代碼08-05

    java調用的幾個方法07-27

    系統調用的概念簡介07-06

    如何調用其他PPT07-17

    JavaScript的應用10-19

    匯編調用C函數10-29

    java構造函數調用技巧10-26

    C語言是如何調用硬件的10-01

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人