Jint Array functions ECMA 5.1

丶灬走出姿态 提交于 2019-12-10 10:53:36

问题


I'm attempting to use Jint (v2.10.4.0) to translate one arbitrary JSON structure to another. However, I am having issues with using map.

According to the ECMA 5.1 language spec, map should exist on Array.prototye. However, when I attempt to use it, I get an error: Jint.Runtime.JavaScriptException: 'Object has no method 'map''

I'm testing this like

Engine engine = new Engine();
var doubles = engine.SetValue("x", "[ 1, 2, 3, 4, 5 ]")
    .Execute("x.map(function(a){ return a + a; })")
    .GetCompletionValue()
    .ToObject();
Console.WriteLine(doubles);
Console.ReadKey();

Ideally, I'd also like to use find, although this is ECMA6. Is there something I'm missing to use Array.Prototype.map or is there a way of introducing polyfills for Jint?


回答1:


Your code is adding a string value as x, so Jint can't find map on the string instance. You probably assumed that the SetValue method was evaluating the parameter as a script but it's actually just assigning a .NET object to a JavaScript varialble.

To assign an array you either need to pass a C# array like SetValue("x", new [] { 1, 2, 3, 4, 5 }) or run the equivalent script like Execute("var x = [1, 2, 3, 4, 5 ]").



来源:https://stackoverflow.com/questions/44236341/jint-array-functions-ecma-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!