<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ottoGoGo 中的 JS 解釋器

          聯(lián)合創(chuàng)作 · 2023-10-02 05:46

          otto 是 用原生 Go 編寫的 JavaScript 解析器和解釋器。

          import (
             "github.com/robertkrimen/otto"
          )

          在 VM 中運(yùn)行

          vm := otto.New()
          vm.Run(`
              abc = 2 + 2;
              console.log("The value of abc is " + abc); // 4
          `)

          獲取 VM 外的值

          if value, err := vm.Get("abc"); err == nil {
              if value_int, err := value.ToInteger(); err == nil {
          	fmt.Printf("", value_int, err)
              }
          }

          設(shè)置數(shù)字

          vm.Set("def", 11)
          vm.Run(`
              console.log("The value of def is " + def);
              // The value of def is 11
          `)

          設(shè)置字符串

          vm.Set("xyzzy", "Nothing happens.")
          vm.Run(`
              console.log(xyzzy.length); // 16
          `)

          獲取表達(dá)式的值

          value, _ = vm.Run("xyzzy.length")
          {
              // value is an int64 with a value of 16
              value, _ := value.ToInteger()
          }

          拋出錯誤

          value, err = vm.Run("abcdefghijlmnopqrstuvwxyz.length")
          if err != nil {
              // err = ReferenceError: abcdefghijlmnopqrstuvwxyz is not defined
              // If there is an error, then value.IsUndefined() is true
              ...
          }

          設(shè)置 Go 函數(shù)

          vm.Set("sayHello", func(call otto.FunctionCall) otto.Value {
              fmt.Printf("Hello, %s.\n", call.Argument(0).String())
              return otto.Value{}
          })

          在 JS 中使用函數(shù)

          result, _ = vm.Run(`
              sayHello("Xyzzy");      // Hello, Xyzzy.
              sayHello();             // Hello, undefined
          
              result = twoPlus(2.0); // 4
          `)
          瀏覽 28
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大香蕉亚洲视频 | 亚洲欧洲免费在线观看 | 五月天黄色电影播放 | 国产精品﹣色哟哟入口 | 日韩人妻在线视频 |