Jint数组功能ECMA 5.1 [英] Jint Array functions ECMA 5.1
问题描述
我正在尝试使用Jint(v2.10.4.0)将一种任意的JSON结构转换为另一种。但是,我在使用 map 时遇到问题。
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.
根据ECMA 5.1语言规范,地图应该存在于Array.prototye上。但是,当我尝试使用它时,出现错误: Jint.Runtime.JavaScriptException:'对象没有方法'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''
我正在像这样测试
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();
理想情况下,我也想使用find,尽管这是ECMA6。是否缺少使用Array.Prototype.map的功能,或者有没有为Jint引入polyfill的方法?
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?
推荐答案
您的代码正在将字符串值添加为 x
,因此Jint在字符串实例上找不到 map
。您可能以为 SetValue
方法将参数作为脚本进行评估,但实际上只是将.NET对象分配给JavaScript变量。
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.
要分配数组,您需要传递C#数组,例如 SetValue( x,new [] {1,2,3, 4,5})
或运行等效脚本,例如 Execute( var x = [1、2、3、4、5])
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 ]")
.
这篇关于Jint数组功能ECMA 5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!