0

I'm editing the main post, originally I was checking a way to dynamically call function name beginning from a couple of strings. Specifically I had a prefix and a variable string based on a text list. Actually I get this words by a table, my purpose is create filters for the datatable jquery object. However I solved with the eval method like this:

var fn = 'my_foo';
eval(fn+'()');

I really have to say I thought to have fixed my issue, but eval is a bad practise for security risks and majorly I'm still not able to test if dynamic function name exists. I have tried $isFunction, window[myname], typeof myname === 'function', nothing of the constructs above works. I also have tried $ instead of window since I'm using jquery and webpack. I'm driving crazy please help.


FINAL: OK I solved, it's hard to explain in my case. I was not able to see the function cause of scope, and I have read tons of example across the internet making several try. Nothing was working for me. Only one result was fitting to my case, I lost the link was here stackoverflow, a guy has described my same situation he just can't find the scope of the target function cause of closures. My scope is the same, I have an index.js, function1.js function2.js function3.js in which every functions are individually exposed with "export" and individual function are used in the peers codes with a prefix like fun1. fun2. fun3. Well, I was working as example in the same function3 where mytarget and mydynfunctioncaller lives. I have tried to use window[fun3.mytarget] but nothing has got succes. How I have fixed? I just have created in example fun1.myrepowithcaseofinside and called it by the function in fun3. That's all.

rivaldid
  • 85
  • 1
  • 11
  • It's possible your original didn't work if the function was scoped (eg inside another object and not directly on `window`). – freedomn-m May 04 '22 at 14:54
  • Please don't update your *question* to include the *answer*. Your current edit does not have a viable question - please explain what you are trying to do and what attempts you've made; ideally why your first solution doesn't work (not the error, but the reason) – freedomn-m May 04 '22 at 14:55
  • How can I print his scope? I have not so confidence with this variable. if I do console.log($scope.find('myname')) I get $scope not defined – rivaldid May 04 '22 at 15:34
  • Where did you get `$scope` from? It's *scope* - a programming concept. – freedomn-m May 04 '22 at 16:00
  • Right, my brain is firing. All the code is in the same library, and the target function is at the same level than the source function so the scope of the caller is in a function which is not in the scope of the target. Can you help me how jump back of 1 level? – rivaldid May 04 '22 at 16:13
  • This could be my solution, but I don't get it working: https://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string?page=1&tab=trending#tab-top – rivaldid May 04 '22 at 16:48

1 Answers1

0

Ok finally I solved with this answer: Jquery dynamically change function name

My code so is:

var fn = 'my_foo';
eval(fn+'()');
rivaldid
  • 85
  • 1
  • 11
  • Please, edit directly your question with updates instead of adding multiple your-own-answers. – cheesyMan May 04 '22 at 12:56
  • "*eval is evil*" - try not to use `eval`. See [here](https://stackoverflow.com/a/86580/2181514) and [here](https://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil) (among other, many, online resources). – freedomn-m May 04 '22 at 14:51
  • Thanks, I've read about this and the reason why evoid but as last try with eval my function get called successfully. I'm also asking about avoid because my main problem is how to test if dynamic name exists and I need to do before the eval command – rivaldid May 04 '22 at 15:39