Javascript函数调用中的变量名 [英] Variable name in function call in Javascript
本文介绍了Javascript函数调用中的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现以下伪代码:
I'm trying to achieve the following pseudo code:
function processAboutLink(){
}
function processServicesLink(){
}
var variableName = 'about';
process + variableName + Link();
var variableName = 'services';
process + variableName + Link();
我知道上面的代码不是真实的,但是是一个逻辑表示。任何人都可以指出我正确的方向吗?
I'm aware that the code above isn't real but is a logical representation. Can anyone point me in the right direction?
推荐答案
拥有一个对象会更方便,因为你可以动态访问属性:
It would be more convenient to have an object, because you can access properties dynamically:
var processLinkFunctions = {
about: function() { ... },
services: function() { ... }
};
然后,就像这样简单:
processLinkFunctions[variableName]();
这与 processLinkFunctions.about()$ c基本相同$ c> if
variableName ===about
。
这篇关于Javascript函数调用中的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文