zoukankan      html  css  js  c++  java
  • 6.游戏特别离不开脚本(4)-应该避免将集合框架对象传给JS

    java map  传给 javascript 不是自动关联的,最好别传啊,遍历起来也麻烦(尽量避开集合框架吧),用数组或者自建一个对象。这里虽然有种方法:

    // build a Map
    Map<String, String> map = new HashMap<String, String>();
    map.put("bye", "now");
    
    // Convert it to a NativeObject (yes, this could have been done directly)
    NativeObject nobj = new NativeObject();
    for (Map.Entry<String, String> entry : map.entrySet()) {
        nobj.defineProperty(entry.getKey(), entry.getValue(), NativeObject.READONLY);
    }
    
    // Get Engine and place native object into the context
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("javascript");
    engine.put("map", nobj);
    
    // Standard Javascript dot notation prints 'now' (as it should!)
    engine.eval("println(map.bye);");
  • 相关阅读:
    前端页面的防抖与节流
    vue3.0 响应式原理
    cssBEM命名规范及常用CSS class 命名
    vue2.x 响应式原理
    npm 相关命令
    Node之 fs
    Node 之 Buffer
    Node之path
    Node之process
    Node.js 如何处理 ES6 模块
  • 原文地址:https://www.cnblogs.com/booke/p/3202730.html
Copyright © 2011-2022 走看看