-4

I need to call a function via a var string. I see that this question has been asked before: How to execute a JavaScript function when I have its name as a string

But the solution is not working. Did I do something wrong? https://jsfiddle.net/puLh9keg/

// a.callThis is the function that will be called using var string
var a = {
  callThis:
     function (ok, param1, param2) {
            alert(ok + "|" + param1 + "|" + param2);
        }
}

// Below is from https://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string 
function executeFunctionByName(functionName, context /*, args */) {
  var args = [].slice.call(arguments).splice(2);
  var namespaces = functionName.split(".");
  var func = namespaces.pop();
  for(var i = 0; i < namespaces.length; i++) {
    context = context[namespaces[i]];
  }
  return context[func].apply(context, args);
}


// try call a.callThis by var string
var fn = 'a.callThis';
executeFunctionByName(fn, window, true, 'param1', 'param2');
Community
  • 1
  • 1
HockChai Lim
  • 1,675
  • 2
  • 20
  • 30
  • 2
    Please tell us what you mean by "not working." – Bill the Lizard Sep 11 '16 at 03:03
  • 1
    Look at how your code is being added to the result window (use dev tools) you will see you arent using the correct JSFiddle settings to make this work – Patrick Evans Sep 11 '16 at 03:08
  • 2
    your `var a` is not in global scope - change to `window.a =` - or ensure that your JS is not running "on load" but "no wrap in body" or "no wrap in head" – Jaromanda X Sep 11 '16 at 03:45
  • 2
    Don't pass functions around using their names. In JavaScript, you can pass the function **itself** around, since functions are first-class objects. However, you may need to ensure the function is bound to the proper `this`. –  Sep 11 '16 at 06:41
  • *I need to call a function via a var string.* Why? Don't pass functions around using their names. In JavaScript, you can pass the function **itself** around, since functions are first-class objects. However, you may need to ensure the function is bound to the proper `this`. –  Sep 11 '16 at 06:44

1 Answers1

1

Your code works as written. As multiple people have mentioned in comments, the reason your JSFiddle does not work is that you you have made the assumption that window is the global scope in which you are operating. However, you have set your JSFiddle JavaScript to run onLoad. This wraps it in a onload handler. Thus, contrary to your assumption, your code is not running with window as the global scope, which makes it not work. You can get your code working on JSFiddle by changing the JavaScript LOAD TYPE option to either No wrap - in <head> or No wrap - in <body>.

Here is a JSFiddle that has that change implemented.

Also, below is your code in a snippet, which is working fine.

// a.callThis is the function that will be called using var string
var a = {
  callThis:
     function (ok, param1, param2) {
            alert(ok + "|" + param1 + "|" + param2);
        }
}

// Below is from 
// http://stackoverflow.com/questions/359788
//      /how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string 
function executeFunctionByName(functionName, context /*, args */) {
  var args = [].slice.call(arguments).splice(2);
  var namespaces = functionName.split(".");
  var func = namespaces.pop();
  for(var i = 0; i < namespaces.length; i++) {
    context = context[namespaces[i]];
  }
  return context[func].apply(context, args);
}


// try call a.callThis by var string
var fn = 'a.callThis';
executeFunctionByName(fn, window, true, 'param1', 'param2');
Makyen
  • 31,849
  • 12
  • 86
  • 121