Javascript 如何在对象上调用字符串中的函数?
我有: 所有这些都运行良好,但此类调用非常多,因此我在阵列中:Javascript 如何在对象上调用字符串中的函数?,javascript,Javascript,我有: 所有这些都运行良好,但此类调用非常多,因此我在阵列中: Main.children.firstfunction.isEnabled = true; Main.children.second.isEnabled = true; Main.children.gsdfgsg.isEnabled = true; Main.children.other.isEnabled = true; 我想: var names = ['firstfunction', 'second', 'gsdfgsg',
Main.children.firstfunction.isEnabled = true;
Main.children.second.isEnabled = true;
Main.children.gsdfgsg.isEnabled = true;
Main.children.other.isEnabled = true;
我想:
var names = ['firstfunction', 'second', 'gsdfgsg', 'other'];
但这当然行不通。如何改进它?使用括号([]
)表示法。这将允许您动态计算属性名称
试一试
for (var name in names) {
Main.children.name.isEnabled = true;
}
for (var name in names) {
Main.children[name].isEnabled = true;
}