在JavaScript中,我有多个不同的模块(对象)和一个名为“one”的函数。你知道吗
test_module1 = {
one: function () {
alert('fun mod1_one successful');
},
two: function () {
alert('fun mod1_two successful');
}
}
test_module2 = {
one: function () {
alert('fun mod2_one successful');
},
two: function () {
alert('fun mod2_two successful');
}
}
workingObj = test_module1;
workingObj["one"]();
现在,如果变量“workingObj”中有一个模块/对象,我想调用 这个对象上的函数“one”,我称之为workingObj[“one”]();。你知道吗
现在我在学Python。这种语言有什么相似之处吗?你知道吗
我需要一个没有Python类/继承的解决方案。你知道吗
提前多谢
沃尔夫冈
当然!您所要做的就是利用“getattr”并执行以下操作
你可以使用^{}
现在可以使用
get_one
从任何对象获取one
,并像这样调用它优于
getattr
除了它的可读性和习惯用法之外,您不必为每个对象调用
methodcaller
,不像getattr
。创建一次,并使用它,只要你想与尽可能多的对象。你知道吗例如
相关问题 更多 >
编程相关推荐