Forum Moderators: open

Message Too Old, No Replies

An array of function pointers

         

Fotiman

6:34 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What I want is a JavaScript object that has an array of JavaScript function calls. I can do this if the functions don't have any parameters, but haven't yet figured out how to do it for functions that take parameters. For example:


function myObj()
{
function add(f)
{
var fcall = f;
if( typeof f!= "function" )
{
fcall = new Function(f);
}
this.funcs[this.funcs.length] = fcall;
}
function execute()
{
for( var i = 0; i < this.funcs.length; i++ )
{
this.funcs[i](); // Call the function
}
}
this.funcs = new Array();
this.add = add;
this.execute = execute;
}


function hello()
{
alert("hello");
}
var foo = new myObj();
foo.add(hello);
foo.execute();

However, suppose I have this function:


function goodbye( name )
{
alert( "Goodbye " + name );
}

How can I add that?

Bernard Marx

8:04 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I add that?

You can add it, but how would you like to specify the argument when it is called?

You call the series of functions like this:

foo.execute();

..but since you don't know what position in the array the function requiring an argument is, you can't specify it. We need a little more information from your good self.

I have messed around with you code a little..
(Changed the constructor function to 'FnArray')

- Avoid defining the methods inside the constructor. This is inneficient, and causes closures (although they're fun too). These methods can be inherited via prototype.

- There is a branch in the 'add' method for defining a function if a string is passed instead of a function (I assumed). The little wiggle with the variable 'fcall' wasn't needed. I have added such a call.


function FnArray()
{
this.funcs = new Array;
}

FnArray.prototype.add = function(f)
{
if( typeof f!= "function" )
{
f = new Function(f);
}
this.funcs[this.funcs.length] = f;
}

FnArray.prototype.execute = function()
{
for( var i=0; i<this.funcs.length; i++ )
{
this.funcs[i]();
}
}

function hello(){ alert("hello"); }
function goodbye( name ){ alert( "Goodbye " + name ); }

var foo = new FnArray();
foo.add(hello);
foo.add("alert('function from string')");
foo.execute();

Fotiman

8:25 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




You can add it, but how would you like to specify the argument when it is called?

I'm thinking of something along the lines of this:


var foo = new myObj();
foo.add(hello);
foo.add(goodbye("Peter")); // That won't work though
foo.execute();

In other words, the argument(s) would be included when the function was added. Also, ideally, it would be dynamic such that I could have any number of arguments, or none at all.

Any ideas?

Bernard Marx

9:02 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. Wrap it in a function:

foo.add( [blue]function(){[/blue] goodbye("Peter")[blue]}[/blue] );

Bernard Marx

9:19 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's also possible using the "string option", but there can be differences (Using the function wrap is probably more flexible).


function goodbye(name){ alert("goodbye "+name); }

function someFunction()
{
var aName = "Eric Local"
foo.add( function(){ goodbye(aName)} );
foo.add( "goodbye(aName)" );
}

var aName = "Paul Global"
var foo = new FnArray();
someFunction()

foo.execute();

Fotiman

10:02 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ooh, I like that! :) Thanks!