Click here to Skip to main content
Click here to Skip to main content

Function Pointers in JScript

, 13 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Basic overview of function variables in jscript
<!-- Add the rest of your HTML here -->

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.

function dostuff()
{
	return 'stuff';
}
var fnpointer=dostuff; //line 5
alert(fnpointer()); // line 6
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 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:

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
function myfnvar()
{
	alert('you called the function variable');
}
function realfunction(ofnvar)
{
	ofnvar();
}
realfunction(myfnvar);

Pretty cool, huh?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

SimonS
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

 
Generalgood!! Pinmemberlove1981ly24-May-07 5:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 14 Jun 2002
Article Copyright 2002 by SimonS
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid