飞镖:从字符串创建方法 [英] Dart: Create method from string

查看:80
本文介绍了飞镖:从字符串创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用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]);

这篇关于飞镖:从字符串创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆