SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
Thread: array of functions
-
Jan 11, 2007, 10:28 #1
- Join Date
- Jan 2005
- Location
- Outerspace
- Posts
- 511
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
array of functions
I was trying to store my functions in an Array but I am puzzled with functions with arguments...
Here is how I came around the problem:
Code:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Nouvelle page 1</title> </head> <script type='text/javascript'> listStart= new Array() ; function addToStart(fnc){ listStart[listStart.length]=fnc; } function start(){ for (i=0;i<listStart.length;i++){ if(listStart[i] instanceof Function) { listStart[i](); } else { eval (listStart[i]); } }//end boucle for }//end function function one(){alert('one')} function two(xx){alert(xx)} addToStart(one) addToStart("two('hello')") </script> <body onload='start()'> </body> </html>
but the other bit I'm really wanting to shoot is the fact that I have to add a sting to the array if the function needs arguments ..
Any smarter way to do this ?Last edited by SpaceFrog; Jan 11, 2007 at 10:46. Reason: tabs in code
-
Jan 11, 2007, 10:36 #2
- Join Date
- Jul 2006
- Posts
- 151
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You may wish to reformat your code so that it is more pleasant to read, it is being jumbled quite a bit right now.
Dave
-
Jan 11, 2007, 10:47 #3
- Join Date
- Jan 2005
- Location
- Outerspace
- Posts
- 511
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
sorry it looks like I had a few tabulations in the copy / paste of my code ...
-
Jan 11, 2007, 10:54 #4
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No need to use eval in this case. Just pass an anonymous function that will contain the function call with an argument:
Code:<script> var listStart = []; function addToStart(fnc) { listStart.push(fnc); } function start() { for(var i=0; i<listStart.length; i++) { listStart[i](); } } function one() { alert("one"); } function two(xx) { alert(xx); } addToStart(one); addToStart(function(){ two("hello"); }); window.onload = start; </script>
-
Jan 15, 2007, 07:43 #5
- Join Date
- Jan 2005
- Location
- Outerspace
- Posts
- 511
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
weird !!!
just because of the () in the onload ???
why doesn't it work with
Code:<body onload="start()">
or
Code:<body onload="start">
Ok got it but I do not understand ...
Code:addToStart(function(){ two("hello"); });
Code:addToStart(two("hello"));
-
Jan 15, 2007, 07:48 #6
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
To call a function you use () after the function name. But what I did above is to assign a function to the event. The window.onload event will then trigger that function when it triggers.
-
Jan 15, 2007, 08:01 #7
- Join Date
- Jan 2005
- Location
- Outerspace
- Posts
- 511
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I don't really get the reason of this ...
Code:addToStart(function(){ two("hello"); });
Code:addToStart(two("hello"); );
-
Jan 15, 2007, 08:03 #8
- Join Date
- Jan 2005
- Location
- Outerspace
- Posts
- 511
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I don't really get the reason of this ...
Code:addToStart(function(){ two("hello"); });
Code:addToStart(two("hello"); );
Anyway thanks for the tip,
I will juste use this notation as it will be universal:
Code:addToStart(function(){one()});
Code:addToStart(function(){ two("hello"); });
-
Jan 15, 2007, 08:41 #9
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
addToStart(function(){ two("hello"); });
causes the function inside to be added to the listStart array. Then when you call start() the "two" function gets executed.
addToStart(two("hello"); );
Calls the two function and adds the return value (it doesnt have one) to the listStart array. When you call start() it might generate an error.
-
Jan 15, 2007, 18:02 #10
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I don't really get the reason why this [statement]:
Code:addToStart(function(){ two("hello"); });
Code:function someFunc() { alert(hello); }
Code:someFunc();
In this line:
Code:addToStart( function(){ two("hello"); } );
You could do this if you were so inclined:
Code:function two(xx){alert(xx)}; var result = function(){two("hello");}( );
two("hello");
to execute. Note: since the anonymous function does not return a value, result will be undefined. Function calls are replaced in the code by their return values.
In this code:
Code:addToStart(two("hello"); );
a) two is not inside a function definition
b) two is followed by the function execution operator
One final point.
Code:function someFunc() { alert("hello"); }
Last edited by 7stud; Jan 15, 2007 at 21:44.
-
Jan 16, 2007, 03:45 #11
- Join Date
- Jan 2005
- Location
- Outerspace
- Posts
- 511
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks for sparing time to make thing clearer for me
Bookmarks