<!-- Add the rest of your HTML here -->
Unless you're from a C/C++ background, function pointers/variables might seem like a strange concept. Basically, a function pointer is just like any other variable out there, but instead of pointing to an integer or string type, it points to the actual function. Bruce Eckel
defines a function pointer as: "Once a function is compiled and loaded into the computer to be executed, it occupies a chunk of memory. That memory, and thus the function, has an address".
To illustrate this, here's a basic usage example.
As you can see there really anything difficult about the source code itself. If you have a closer look at the code, you'll see that line 5
is creating a variable
. Now, obviously, this isn't one of the JScript intrinsic type. The variable
is actually referencing the function
and is used as a direct call to
would be ( viz
Here's an example where parameters are being used:
return 'stuff' + sblah;
The variable fnpointer
acts just like the function does. So what's the point? ...
Well, callbacks now become a possibility.
What you can also do is pass a function variable to a function and the function will be able to call the function this function variable points to, without much knowledge about the actual function. One thing that true OO languages would do at this point is use interfaces. JScript doesn't have these.
Here's an elementary example of this
alert('you called the function variable');
Pretty cool, huh?