飞镖:从字符串创建方法

编程入门 行业动态 更新时间:2024-10-26 15:18:49
本文介绍了飞镖:从字符串创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在尝试使用Dart从字符串动态创建方法,但无济于事。字符串示例:((String str)=> return str.length;。这个想法是允许用户创建自己的函数以应用于给定的字符串。我发现的唯一东西是NoSuchMethod,它似乎不适用于我的情况。我尝试在JavaScript中使用新函数,但是将函数传递给Dart并执行它时,出现以下错误: 未捕获的TypeError:J. $ index $ asx(...)。call $ 0不是函数 。

I've been trying to create methods dynamically from strings using Dart to no avail. String example: "(String str) => return str.length;". The idea is to allow users to create their own functions to apply to a given string. The only thing I've found is NoSuchMethod which does not seem to apply to my case. I tried using new Function in JavaScript but when passing the function to Dart and executing it, I get the following error: Uncaught TypeError: J.$index$asx(...).call$0 is not a function.

代码示例:

Dart:

context["UpdateNames"] = (JsObject pTag) { print(pTag["function"]("text")); };

JS:

function execute () { var func = {"function": new Function("str", "return str.length;")}; UpdateNames(func); }

编辑:

解决方案:在JavaScript中创建一个这样的对象:

Solution: Create an object in JavaScript such as this:

this.fun = function (name) { var text = "var funs = " + document.getElementById("personalFun").value; eval(text); return funs(name); };

然后在Dart中创建对象:

Then create the object in Dart:

caller = new JsObject(context['Point'], []);

最后调用该方法以动态创建函数:

Finally call the method to dynamically create the function:

caller.callMethod('fun', [text]);

推荐答案

解决方案:在JavaScript中创建如下对象:

Solution: Create an object in JavaScript such as this:

var FunctionObject = function() { this.fun = function (name) { var text = "var funs = " + document.getElementById("personalFun").value; eval(text); return funs(name); }; };

然后在Dart中创建对象:

Then create the object in Dart:

caller = new JsObject(context['FunctionObject'], []);

最后调用该方法以动态创建函数:

Finally call the method to dynamically create the function:

caller.callMethod('fun', [text]);

更多推荐

飞镖:从字符串创建方法

本文发布于:2023-10-28 02:07:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:飞镖   字符串   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!