3

This is either a dumb question or one without an answer

I want to call a function by using an element in an array as a way to match the function name. The elements of the array are, and may need to remain string.

i.e

   $(document).on('click', 'a', function() {
      var classes = $(this).attr('class'); 
      var classesArr = classes.split(' ');
      
      arr[1]();
   });

   function two() { console.log('hello'); }
   <a href="foo" class="one two three"></a>

obviously this doesn't work, and I get "arr[1] is not a function". But is there a trick?

3
  • The question has no relation to jQuery. I removed the tag accordingly. Commented Oct 23, 2022 at 7:27
  • 1
    Does this answer your question? How to execute a JavaScript function when I have its name as a string Commented Oct 23, 2022 at 10:56
  • How you store/access the string doesn't change the fact that this is a duplicate. Commented Oct 23, 2022 at 10:57

3 Answers 3

2

You can use eval() to do it

var arr = ['one', 'two', 'three'];
eval(arr[1]+'()');

function two() { console.log('hello'); }

Or using window

var arr = ['one', 'two', 'three'];
window[arr[1]]();

function two() { console.log('hello'); }

Sign up to request clarification or add additional context in comments.

4 Comments

I thought eval() was to be avoided? I found this as an answer but so many people seemed to say to avoid eval() at all costs as it can be used maliciously. Just curious
@Then you might be using windows,see use-dynamic-variable-names-in-javascript
do you personally agree that using eval() is a poor idea?
@JustinRiley It depends on how you using it,there is not absoultely answer for eval is ppor or not
2

Put the function references into the array - instead of their names as strings.

const arr = [one, two, three];
arr[1]();

function one() {console.log('hello one'); }
function two() {console.log('hello, this is two!'); }
function three() {console.log('hello three'); }

1 Comment

The array elements are string because the source of the array is a string. Like so: var classes = $(this).attr('class'); var classesArr = classes.split(' '); The value of 'class' on link elements are string, e.g 'nav content sub_nav'. Hence the 'jquery' tag
0

You can use a functions Object and declare functions from that. Global functions are always a property of window (the global scope in a browser), so you can refer to them using window[functionName]

const functions = {
  one: _ => console.log(`function one`),
  two: _ => console.log(`function two`),
  three: _ => console.log(`function three`)
};

const [one, two, three] = Object.values(functions);
// call 'one'
one();

// or (not advisable) as global (so: window) properties
const names = [`winOne`, `winTwo`, `winThree`];
const [win1, win2, win3] = names.map(n => window[n]);

// call 'win3'
win3();

function winOne() { console.log(`function winOne`); }
function winTwo() { console.log(`function winTwo`); }
function winThree() { console.log(`function winThree`); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.