Javascript Dart:从字符串创建方法
我一直在尝试使用Dart从字符串动态创建方法,但没有成功。字符串示例:“(字符串str)=>返回str.length;”。其思想是允许用户创建自己的函数来应用于给定的字符串。我唯一发现的是这种方法似乎不适用于我的情况。我尝试在JavaScript中使用新函数,但当将函数传递给Dart并执行它时,我得到以下错误:未捕获类型错误:J.$index$asx(…)。调用$0不是函数 代码示例: 省道: JS: 编辑:Javascript Dart:从字符串创建方法,javascript,dart,Javascript,Dart,我一直在尝试使用Dart从字符串动态创建方法,但没有成功。字符串示例:“(字符串str)=>返回str.length;”。其思想是允许用户创建自己的函数来应用于给定的字符串。我唯一发现的是这种方法似乎不适用于我的情况。我尝试在JavaScript中使用新函数,但当将函数传递给Dart并执行它时,我得到以下错误:未捕获类型错误:J.$index$asx(…)。调用$0不是函数 代码示例: 省道: JS: 编辑: 解决方案:在JavaScript中创建一个对象,如下所示: this.fun = fu
解决方案:在JavaScript中创建一个对象,如下所示:
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
var FunctionObject = function() {
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
};
然后在Dart中创建对象:
caller = new JsObject(context['Point'], []);
caller = new JsObject(context['FunctionObject'], []);
最后调用该方法动态创建函数:
caller.callMethod('fun', [text]);
caller.callMethod('fun', [text]);
我不确定是否完全理解您想要实现的目标,因此我将尝试提供最佳答案 您希望动态地将方法添加到具有特定字符串标识符的类中 在这种情况下,这是完全可能的,但您需要使用一些镜像,因此如果您想在web上使用此镜像 下面是一个实现示例:
import "dart:mirrors";
class Test {
Map<String, dynamic> _methods = {};
void addMethod(String name, var cb) {
_methods[name] = cb;
}
void noSuchMethod(Invocation inv) {
if (inv.isMethod) {
Function.apply(_methods[MirrorSystem.getName(inv.memberName)], inv.positionalArguments);
}
}
}
void testFunction() {
for (int i = 0; i < 5; i++) {
print('hello ${i + 1}');
}
}
void testFunctionWithParam(var n) {
for (int i = 0; i < 5; i++) {
print('hello ${i + n}');
}
}
void main() {
var t = new Test();
t.addMethod("printHello", testFunction);
t.addMethod("printHelloPlusN", testFunctionWithParam);
t.printHello();
t.printHelloPlusN(42);
}
导入“省道:镜像”;
课堂测试{
Map_方法={};
void addMethod(字符串名称,var cb){
_方法[name]=cb;
}
void noSuchMethod(调用inv){
if(inv.isMethod){
函数.apply(_方法[MirrorSystem.getName(inv.memberName)],inv.positionarguments);
}
}
}
void testFunction(){
对于(int i=0;i<5;i++){
打印('hello${i+1}');
}
}
void testFunctionWithParam(变量n){
对于(int i=0;i<5;i++){
打印('hello${i+n}');
}
}
void main(){
var t=新测试();
t、 addMethod(“printHello”,testFunction);
t、 addMethod(“PrintHelloPlus”,testFunctionWithParam);
t、 printHello();
t、 printhellopuns(42);
}
您希望在字符串中“执行”代码
对不起,这是不可能的。这是一个很大的功能需求,但dart团队并没有计划,因为它将涉及到许多更改和折衷
也许可以通过创建dart文件并使用来运行它来欺骗用户。解决方案:在JavaScript中创建一个对象,如下所示:
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
var FunctionObject = function() {
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
};
然后在Dart中创建对象:
caller = new JsObject(context['Point'], []);
caller = new JsObject(context['FunctionObject'], []);
最后调用该方法动态创建函数:
caller.callMethod('fun', [text]);
caller.callMethod('fun', [text]);
谢谢你的回答。我想做的正是第二点。我通过调用一个JavaScript方法实现了它,这让我很难过,因为我希望能够在Dart中完成它。