我一直在尝试使用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]);更多推荐
飞镖:从字符串创建方法
发布评论