2

I want to pass two function names to a function with JSON, but can't get it to work. I'm in over my head with this stuff.

Here are my functions. myFunc is supposed to call the two functions that are passed to it (myBefore and myAfter).

function myFunc(obj) {
  var func = $.parseJSON(obj);
  if (typeof func[before] === "function") func[before]();
  // do some stuff
  if (typeof func[after] === "function") func[after]();
}

function myBefore() {
  alert("before");
}

function myAfter() {
  alert("after");
}

and here is how I'm calling myFunc

myFunc({"before": "myBefore", "after" :"myAfter"});
halfer
  • 19,824
  • 17
  • 99
  • 186
Damian
  • 1,652
  • 4
  • 26
  • 44

5 Answers5

0

How about this? window["yourfunctionname"]();

      var FunctionName =  func[before];         
      if (typeof(window[FunctionName]) === "function")
     {
        window[FunctionName]();
      }

OR

    function myFunc(obj) {
      var func = $.parseJSON(obj);
      if (func[before] === "myBefore") { before();}
      // do some stuff
      if (func[after] === "myAfter") { after(); }
    }
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Something like...

    function myFunc(obj) {
var func = $.parseJSON(obj);
      if (typeof func[before] === "function") func.before();
      // do some stuff
      if (typeof func[after] === "function") func.after();
    }
xspydr
  • 3,030
  • 3
  • 31
  • 49
0

myFunc is supposed to call the 2 functions that are passed to it

You need to check it like func["before"], As per your implementation func[before] is a string not a function

function myFunc(func) {
    //You don't need to parse. As its already a JSON
    //var func = $.parseJSON(obj); 

    if (typeof func["before"] === "function") 
        func["before"]();

    // do some stuff

    if (typeof func["after"] === "function") 
        func["after"]();
}

var myBefore = function () {
    alert("before");
}

var myAfter = function () {
    alert("after");
}

myFunc({
    "before": myBefore,
    "after": myAfter
});

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks for the working demo Satpal. Can you explain why the functions myBefore and myAfter are assigned as variables, and is it possible to just create these as functions as in my original code? – Damian Jan 17 '14 at 19:41
  • @Damian, I prefer it that way. Using Named function will also work Example: http://jsfiddle.net/HBLA4/1/. For more info visit http://stackoverflow.com/questions/114525/the-difference-between-the-two-functions-function-x-vs-var-x-function – Satpal Jan 17 '14 at 19:52
  • That works perfectly! I actually prefer your answer to Kevin's...I think the code looks cleaner. If I could award you vest answer I would, but I've already accepted Kevin's :( – Damian Jan 17 '14 at 20:23
0

You are not calling a funciton. What you are really doing is

("myBefore")();

which is an error.

If myBefore and myAfter functions are in global scope you can use window["stringName"].

function myFunc(obj) {
    //var func = $.parseJSON(obj);  //if it is object being passed in than this is an error
    var func = obj;
    window[func[before]]();
    window[func[after]]();
} 
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Since the functions are declared globally you can obtain them from the global object (window).

function myFunc(obj) {
  if (typeof window[obj.before] === "function") window[obj.before]();
  // do some stuff
  if (typeof window[obj.after] === "function") window[obj.after]();
}

function myBefore() {
  alert("before");
}

function myAfter() {
  alert("after");
}

myFunc({"before": "myBefore", "after" :"myAfter"});

JS Fiddle: http://jsfiddle.net/wvPgA/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189