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