Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable name in function call in Javascript

Tags:

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?

like image 202
benhowdle89 Avatar asked Mar 02 '26 01:03

benhowdle89


1 Answers

It would be more convenient to have an object, because you can access properties dynamically:

var processLinkFunctions = {
  about:    function() { ... },
  services: function() { ... }
};

Then, it's as easy as:

processLinkFunctions[variableName]();

This is basically the same as processLinkFunctions.about() if variableName === "about".

like image 78
pimvdb Avatar answered Mar 03 '26 14:03

pimvdb