SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Extending JavaScript with Function Pointers
By Scott Isaacs

This article originally arose out of our goal to create a cross-browser version of the setInterval method. When we finished writing the scripts we realized that we took advantage of some advanced JavaScript concepts. In this article we focus function pointers and how to use them to extend JavaScript as well as improve cross-browser compatibility. We are going to quickly cover a lot of ground in this article and introduce the following concepts:

  • Explain the function data type
  • Demonstrate using function pointers to create objects
  • Using function pointers to override built-in methods

First we provide background on creating and using function pointers. After this introduction, we shift gears and demonstrate how function pointers can be used to solve cross-browser compatibility issues.

To demonstrate we create a setInterval method that works in Netscape Navigator 3.0 and later and Internet Explorer 4.0 and later. While both the 4.0 browsers support a similar setInterval method their are a few key differences. We resolve those differences by overriding Internet Explorer's setInterval implementation with a Netscape 4.0 compatible one. Also, since function pointers are supported by Netscape Navigator 3.0 we also are able to enhance the 3.0 browser with this additional feature.

First we start by introducing how functions are treated as a standard data-type in JavaScript.