0

我在下面写了代码

static void Main(string[] args)
    {
        var engine = new Engine();
        var file = File.ReadAllText(@"...\Desktop\beautify.js");
        var final = file + Environment.NewLine + "(function () {return js_beautify('var a = 2;');})();";
        var exec = engine.Execute(final);
        var r = exec.GetCompletionValue().ToString();
        Console.WriteLine(r);
        Console.Read();

    }

但 Jint 显示错误

Jint.Runtime.JavaScriptException: 'js_beautify is not defined'

谁能帮助我如何注册外部 js 库,如 beautify.js,然后在 Jint 中调用它们的函数?

4

1 回答 1

1

它应该是

 var engine = new Engine().Execute("var global = {};").Execute(content);
 var js_beautify = engine.Execute("global.js_beautify").GetCompletionValue();
 var result = engine.Invoke(js_beautify, "var a = 2;").AsString();
于 2017-08-16T19:44:10.043 回答