The resource loading... loading...

2.9 Debugging in the run of the strategy robot (JS - the usefulness of the eval function)

Author: Inventors quantify - small dreams, Created: 2016-11-15 14:54:40, Updated: 2017-10-11 10:21:40

Debugging in interaction with the JS eval function.

  • Let's first look at the introduction of the eval function in JS:

    w3school img img

    Now that you know about the eval function, let's look at the implementation of the code in terms of policy interaction!

  • The implementation of strategic interaction:

    The policy interaction requires the user to handle the return value of the API GetCommand function.

var cmd = GetCommand();             // 调用API  获取界面交互控件的消息。 
if (cmd) {                          // 判断是否有消息
    var js = cmd.split(':', 2)[1];  // 分割 返回的消息 字符串, 限制返回2个, 把索引为1的 元素 赋值给 名为js 的变量 
    Log("执行调试代码:", js);         // 输出 执行的代码
    try {                           // 异常检测
        eval(js);                   // 执行 eval函数, 该函数执行传入的参数(代码)。
    } catch(e) {                    // 抛出异常
        Log("Exception", e);        // 输出错误信息
    }
}

Below we write this code into a policy and configure the interactive interface controls. The full test code:

var price = 0;
var amount = 0;
function main() {
    Log("初始 price:", price, "初始 amount", amount);
    while(true){
        var cmd = GetCommand();             // 调用API  获取界面交互控件的消息。 
        if (cmd) {                          // 判断是否有消息
            var js = cmd.split(':', 2)[1];  // 分割 返回的消息 字符串, 限制返回2个, 把索引为1的 元素 赋值给 名为js 的变量 
            Log("执行调试代码:", js);         // 输出 执行的代码
            try {                           // 异常检测
                eval(js);                   // 执行 eval函数, 该函数执行传入的参数(代码)。
            } catch(e) {                    // 抛出异常
                Log("Exception", e);        // 输出错误信息
            }
        }
        Sleep(1000);
    }
}

Added interactive controls:img

  • Run it:

    imgWe're going to change the global variable price, amount.imgIf the code is wrong, it will throw an exception:img

More

Light cloudsThanks to Dream Big, I finally found a way to interact.