-2

I have some JavaScript functions in each page that I call them after jQuery is loaded.
The function in Questions.aspx page is afterQuestions(), the function in Default.aspx is afterDefault() and so on ....

In my master page I am calling them like:

if(typeof(afterQuestion) == 'function') afterQuestions();
if(typeof(afterDefault) == 'function') afterDefault();

As the number of functions grew, I tried something like:

var _fs = [After, AfterDefault,  afterSettings, afterQuestions];
for (var i = 0; i < _fs.length; i++) if (typeof (_fs[i]) == "function")  _fs[i](); 

But it doesn't work this way. Can you please help me how can I create an Array of functions and call them?

Edit: I think nobody had read the question well to see that the all functions won't exist at the same time and that was the problem creating the array. I solved it by adding created functions to a global array and the looping and excuting functions in that array

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171

1 Answers1

1

If the function is not defined, you will get a JavaScript error since you are trying to reference a variable that is not defined. The type of check in your original code gets around that issue.

Your best bet is to namespace the functions into an object and use dot notation.

var methods = {
    After : function () {console.log("After"); },
    AfterDefault : function () {console.log("AfterDefault"); },
    afterSettings : function () {console.log("afterSettings"); }
};

var _fs = [methods.After, methods.AfterDefault,  methods.afterSettings, methods.afterQuestions];
for (var i = 0; i < _fs.length; i++) {
    if (typeof (_fs[i]) == "function")  {
       _fs[i]();
    }
}

Now when you want to register the methods you can just add to the methods object.

methods = methods || {};
methods.afterSettings = function () {  console.log("added this in"); };

Now if the method is not defined the namespace will return undefined and the check will not error out.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 1
    Thanks for the downvote! This is the answer since I talked with the OP and worked on the problem since you folks who closed it did not read the question. – epascarello Jul 02 '15 at 15:26
  • but the problem is that each function is in a separate page I can not define them all in one namespace. I created global array in the head of my master page like : `var afterFuncs = [];` and in every page when I create a function I'm adding it to this array: `var a = function afterDefault(){...} afterFuncs.push(a);` now at the bottom of my master page I just call them all in a loop and even don't need to check if they exist. `for(var i=0;i – Ashkan Mobayen Khiabani Jul 02 '15 at 15:27
  • @AshkanMobayenKhiabani If order of them being called does not matter than that is a okay solution. Unless it is guaranteed that the way you push them to the array is how you want them to be called. You can do it all in one namespace if the namespace is global. You just add the method to the object as I noted above. – epascarello Jul 02 '15 at 15:30
  • Thanks a lot. your answer does not exactly match with my code and thats because you don't have all the codes. but your answer helped me find the error and also give me the clue to solve it. Thanks again dude – Ashkan Mobayen Khiabani Jul 02 '15 at 15:32
  • hey people! do you have a problem? why do you keep down voting the correct answer that actually works? – Ashkan Mobayen Khiabani Jul 02 '15 at 15:35