Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Javascript

Function Pointers in JScript

Rate me:
Please Sign up or sign in to vote.
3.63/5 (16 votes)
13 Jun 2002CPOL1 min read 88.6K   18   3
Basic overview of function variables in jscript

Introduction

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.

JavaScript
function dostuff()
{
	return 'stuff';
}
var fnpointer=dostuff; //line 5
alert(fnpointer()); // line 6

As you can see, there really isn't 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 fnpointer of type dostuff. Now, obviously, this isn't one of the JScript intrinsic type. The variable fnpointer is actually referencing the function dostuff and is used as a direct call to dostuff() would be ( viz Function(param1, param2) ).

Here's an example where parameters are being used:

JavaScript
function dostuff(sblah)
{
	return 'stuff' + sblah;
}
var fnpointer=dostuff;
alert(fnpointer(' mkay')); //line 6

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:

JavaScript
function myfnvar()
{
	alert('you called the function variable');
}
function realfunction(ofnvar)
{
	ofnvar();
}
realfunction(myfnvar);

Pretty cool, huh?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
South Africa South Africa
* Visual C# MVP 2004, 2005 - South Africa
* SADeveloper.NET User Group co-founder and lead 2003, 2004, 2005

MSN : simon_stewart AT hotmail.com
Email : simon AT brokenkeyboards.com
Skype: brokenkeyboards
CEO of Broken Keyboards Software



Founder of these startups:


Browse This For Me


Monitor My URL



My full CV can be download here in PDF format.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Øyvind Sean Kinsey30-Jun-09 7:01
Øyvind Sean Kinsey30-Jun-09 7:01 
Generalgood!! Pin
love1981ly24-May-07 4:07
love1981ly24-May-07 4:07 
Generalremark Pin
zproxy8-Jul-05 1:11
susszproxy8-Jul-05 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.