0

I have a string that I'm trying to use as the name of a function in Jquery 2.2. Here's what that looks like:

var barBaz = 'myFunctionName';

Then I have a function which I want to call, which looks like this:

myFunctionName.changeView('success');

When I call this function directly, it works. But when I try this:

[barBaz].changeView('success');

I get this error:

Uncaught TypeError: [...].changeView is not a function

Obviously I'm missing something in my understanding of the syntax to use a string as the name of a function. (I also attempted the window[barBaz] approach with no better success) Where am I going wrong?

Feature
  • 163
  • 1
  • 1
  • 6
  • Terminology: In your example that "works" - `myFunctionName` is *not* a function, it's an object. – freedomn-m Dec 05 '22 at 15:44
  • Ohhhh. OK, that makes sense then! Thanks! So, how can I convert this string to the name of the object? – Feature Dec 05 '22 at 15:45
  • @freedom-m : Good question, I guess not? myFunctionName is part of a js file that's loaded from a 3rd party service onto the site. – Feature Dec 05 '22 at 15:46
  • @freedom-m : if I simply name the function directly by directly just typing in: myFunctionName.changeView('success'); then it works as it should. I just need to be able to use a variable to change that first part of the function because it changes based on page data. – Feature Dec 05 '22 at 15:50
  • 1
    This question appears to be asking the same thing: https://stackoverflow.com/questions/2146173/how-to-get-local-variable-by-its-name-in-js – freedomn-m Dec 05 '22 at 15:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250166/discussion-between-feature-and-freedomn-m). – Feature Dec 05 '22 at 15:54
  • 1
    @freedomn-m — "[barBaz] is the same as window[barBaz]" — No, it isn't. `[barBaz]` is the same as `new Array(barBaz)`. – Quentin Dec 05 '22 at 16:04
  • @Quentin you are correct - I did also realise this later after adding the comment - but failed to update. Thanks for the correction. – freedomn-m Dec 05 '22 at 16:07

1 Answers1

0

It turns out that since this function was from a 3rd party js file, I needed to use eval() so the final result looked like this:

var barBaz = 'myFunctionName';
eval(barBaz).changeView('success');
Feature
  • 163
  • 1
  • 1
  • 6
  • 1
    **Don't use eval** Glad you got it sorted, but don't thank me for "suggesting eval" - that's not an ideal solution. You'll be better off just moving/repeating your logic that defines barBaz in the first place. eg instead of `barBaz = (check_failed ? 'myFunctionFailed' : 'myFunctionPassed')` - set it to a reference of the object itself, eg `bazBaz = (check_failed ? myFunctionFailed : myFunctionPassed)`. Don't use eval. – freedomn-m Dec 05 '22 at 16:11
  • Related question: [Why is using the JavaScript eval function a bad idea?](https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) – Yogi Dec 05 '22 at 16:16
  • Sorry, credited you because your comment seemed to point to a thread where eval() was the answer...and it worked. I don't really understand how to do what you suggest in the above comment in my situation... – Feature Dec 05 '22 at 16:27
  • TBH if it works for you and you understand the risks, then don't worry. We would need to see how you're setting `var barBaz = 'myFunctionName';` to help more. – freedomn-m Dec 05 '22 at 16:51
  • I feel like the risk is mitigated a bit because barBaz is a combo of a string in an html data attribute (which would be risky on its own) and then I tack on another 'fixed' string to that. So, the data attribute may be: 123456, but then I take that an always tack on "abcd" to the front end, resulting in eval(abcd123456). Then all this becomes the object name. Feels low risk from how I understand it...although I'm trying to figure out a way to get the string somewhere invisible, instead of in a data attribute...Thanks again for your help. – Feature Dec 05 '22 at 16:55