Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript通过变量名调用函数内部的函数_Javascript_Function_Object_Dynamic - Fatal编程技术网

JavaScript通过变量名调用函数内部的函数

JavaScript通过变量名调用函数内部的函数,javascript,function,object,dynamic,Javascript,Function,Object,Dynamic,当函数名位于变量中时,我很难在另一个函数中调用函数: var obj = {} obj.f = function() { var inner = { a: function() { function b() { alert('got it!'); } b(); // WORKS AS EXPECTED x = 'b'; [x](); // DOESN'T WORK, NEITHER this[x]() wi

当函数名位于变量中时,我很难在另一个函数中调用函数:

var obj = {}

obj.f = function() {
  var inner = {
    a: function() {
      function b() {
        alert('got it!');
      }
      b(); // WORKS AS EXPECTED
      x = 'b';
      [x](); // DOESN'T WORK, NEITHER this[x]() window[x](), etc.
    }
  }
  inner.a();
}

obj.f();
我尝试用不同的作用域路径作为
[x]()
的前缀,但到目前为止没有成功。搜索现有答案没有找到任何结果。如果将
b()
直接放置在对象内部,则它可以使用
此[x]()
。我想将
b()
保留为
函数a()
中的一个函数,因为
函数a()
中存在变量作用域,否则我需要将许多参数传递给
b()

////
重复问题:昆汀在这个帖子中提供了一个更优雅的答案。

试试这个。当前,您正在将字符串指定给变量x,而不是函数变量

 x = b;
 x();

使用与变量名称匹配的字符串访问任意变量是不明智的。(有关非常糟糕的方法,请参见
eval

[x]()//不起作用

您正在尝试将数组作为函数调用

这[x]()

函数不是
内部对象的属性

窗口[x]()

因为它不是全局的,所以也不是窗口对象的属性


如果您需要基于字符串变量的值调用函数,那么请在对象中组织函数并从该对象访问它们

  function b() {
    alert('got it!');
  }
  var myFunctions = {
      b: b
  };
  x = 'b';
  myFunctions[x](); 
函数声明
b
将在指定为
a
的匿名函数表达式的闭包中捕获

  • 当在闭包中使用
    var
    时,没有(在JavaScript中可用)对象被分配类似于全局范围中的
    window
    的属性
  • 有效地编写函数声明
    var
    s函数的名称
  • 如果您确实希望通过字符串访问变量(即
    b
    ),则需要创建一个包含
    b
    的对象,该对象类似于您为
    a
    所做的操作,或者(可能是危险的)依赖
    eval
    “b”
    转换为
    b

    如果不能提前创建整个对象,可以使用此格式

    /* in (higher?) scope */
    var fnRef = {};
    // now when you
    function b() {/* define as desired */}
    // also keep a ref.
    fnRef['b'] = b;
    // fnRef['b']() will work **after this line**
    

    假设您的代码如下所示:

    //instead of x = 'b'
    x = function(){}
    
    var obj = {}
    
    obj.f = function() {
      var inner = {
        a: function() {
          function b() {
            alert('got it!');
          }
          b(); // WORKS AS EXPECTED
          //you can define it as a variable
          var x = function(){};
          //and call it like this
          x();
    
          //or define it like this
          this[x] = function(){};
          //and call it like this
          this[x]();
    
          //but you are creating an array [x] which is not a function
          //and you are trying to call an array????
          [x](); // DOESN'T WORK, NEITHER this[x]() window[x](), etc.
        }
      }
      inner.a();
    }
    
    obj.f();
    
    那么您的解决方案可能是这样的:

    //instead of x = 'b'
    x = function(){}
    
    var obj = {}
    
    obj.f = function() {
      var inner = {
        a: function() {
          function b() {
            alert('got it!');
          }
          b(); // WORKS AS EXPECTED
          //you can define it as a variable
          var x = function(){};
          //and call it like this
          x();
    
          //or define it like this
          this[x] = function(){};
          //and call it like this
          this[x]();
    
          //but you are creating an array [x] which is not a function
          //and you are trying to call an array????
          [x](); // DOESN'T WORK, NEITHER this[x]() window[x](), etc.
        }
      }
      inner.a();
    }
    
    obj.f();
    

    问题在于你的作业

    使用
    x=b
    而不是
    x='b'
    分配函数对象,因为后者只是将字符串分配给x

    一旦您修复了分配,您就可以根据需要调用函数

    x();
    a.x();
    a[x]();
    

    等等。

    您应该为函数创建数组,然后使用变量中的名称进行访问,如下所示:

    var obj = {}
    
    obj.f = function() {
      var inner = {
        a: function() {
          // set up the possible functions:
          var myFuncs = {
                  b: function b() {alert('got it!');}
                  };
          //b(); // WORKS AS EXPECTED --> commented this code
          x = 'b';
          myFuncs[x]();    
        }
      }
      inner.a();
    }
    

    尝试
    窗口[x]。应用(此[param1,param2])
    @RaraituL
    b
    将被限定范围,因此您无法执行此操作this@PaulS. 定义
    x
    的方式是,它将在
    窗口下创建
    对象,因此您想在何处调用什么函数?@RaraituL-
    x
    将是,但
    b
    不会。不存在的是
    window['b']
    ,而不是
    x
    ;成功了@Gonki对于这个解决方案,您需要在代码中进行最小的更改。如果直接指定值b,它可能会起作用。它来自一个计算。示例中的代码不是真实代码。您必须将
    a[x]()
    更改为
    a[“x”]()
    ,除非x是一个字符串变量,其中包含
    “x”
    值。