Click here to Skip to main content
13,860,478 members
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 10 Apr 2002
Licenced CPOL

Function Pointers in JScript

, 13 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Basic overview of function variables in jscript


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 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:

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)

Pretty cool, huh?


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


About the Author

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
Email : simon AT
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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Øyvind Sean Kinsey30-Jun-09 8:01
memberØyvind Sean Kinsey30-Jun-09 8:01 
Generalgood!! Pin
love1981ly24-May-07 5:07
memberlove1981ly24-May-07 5:07 
Generalremark Pin
zproxy8-Jul-05 2:11
susszproxy8-Jul-05 2: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 14 Jun 2002
Article Copyright 2002 by SimonS
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid