用新函数构造的javascript函数无法调用其他函数 [英] javascript Function constructed with new Function cannot call other functions

查看:136
本文介绍了用新函数构造的javascript函数无法调用其他函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android上使用React-native.但是,我认为我的问题适用于任何JavaScript环境.

Using React-native on android. However, I think my question applies to any javascript environment.

我正在从服务器发送的文本中构造一个函数(有充分的理由).

I am constructing a function from text sent from the server (there are good reasons for it).

function helper_called_from_dynamic (arg1)
{
  console.log('helper called ',arg1);
}

export class MyInvoker
{
  constructor ()
  {
    this._funcProps={};
  }

  initialize ( item )
  {
    this._funcProps["df1"]=new Function (["inArg1"],item.fnBody);
  }

  call_dynamic_func (fnName,arg1)
  { 
    return this._funcProps[fnName](arg1);
  }
}

fnBody具有以下内容: "return helper_drawn_from_dynamic(inArg1);"

The fnBody has the following: " return helper_called_from_dynamic(inArg1); "

以下是我通过My​​Invoker进行的调用

my invocation via MyInvoker is the following

let invInst = new MyInvoker();
let item={fnBody:"return helper_called_from_dynamic(inArg1); "};
invInst.initialize(item);
invInst.call_dynamic_func("df1","somearg");

我收到一个错误(来自react-native,但同样,我怀疑这对于所有其他JavaScript环境都是常见的):

I am getting an error (from react-native, but again, I suspect it would be common to all other javascript environments):

找不到变量:helper_drawn_from_dynamic

cannot find variable: helper_called_from_dynamic

是否可以使它正常工作?那是否允许动态创建的函数调用其他函数? 还是我必须求助于评估"?

Is it possible to get this to work? That is allowing the dynamically created functions to call other functions? Or do I have to resort to 'eval' ?

推荐答案

@Bergi提示的解决方案为我工作. 建议

Solution hinted by @Bergi worked for me. The suggestion

使逃避的代码仅调用参数的方法

Make the evaled code only call methods of arguments

是我所做的,并且有效.我应该在发布之前考虑过它,但是那时我还没有想到. 我没有将辅助函数设为全局,而是将其附加到类的实例上,然后将该实例作为参数传递给动态函数.

is what I had done, and that worked. I should have thought about it before posting, but it had not occured to me at the time. Instead of making the helper function global, I attached it to an instance of a class, and passed that instance as an argument to the dynamic function.

这是细节

export class MyHelpers
{
 helper_called_from_dynamic (arg1)
 {
   console.log('helper called ',arg1);
 }
}

export class MyInvoker
{
  constructor ()
  {
    this._funcProps={};
    this._myHelpers=new MyHelpers();
  }

  initialize ( item )
  {
    this._funcProps["df1"]=new Function (["inArg1","iHelper"],item.fnBody);
  }

  call_dynamic_func (fnName,arg1)
  { 
    return this._funcProps[fnName](arg1,this._myHelpers);
  }
}

然后,动态函数的主体现在可以访问helper_drawn_from_dynamic了:

Then the body of the dynamic function now can access the helper_called_from_dynamic :

let invInst = new MyInvoker();
let item={fnBody:"return iHelper.helper_called_from_dynamic(inArg1); "};
invInst.initialize(item);
invInst.call_dynamic_func("df1","somearg");

这篇关于用新函数构造的javascript函数无法调用其他函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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