JavaScript Functions As a Function Argument

JavaScript Functions As a Function Argument thumbnail
JavaScript lets programmers bring object oriented coding techniques to the Web.

Programmers on the web use JavaScript as a flexible and functional programming language for the web. JavaScript's flexibility comes from the fact that programmers can use many traditional programming paradigms as part of its interface, but can still embed JavaScript throughout HTML documents. One of these paradigms involves the use of object oriented programming techniques, and enables programmers to use functions as parameters (or arguments) for other functions.

  1. Functions and Arguments

    • In computer programming, a function defines a block of code that performs a specific task. A programmer can then call the code by its name in order to perform that task. In JavaScript, for example, a programmer might define a function like this example:

      function doubleNum(x)

      {

      return x*2;

      }

      The "x" in parentheses is what is called an "argument," or a variable that a programmer supplies to the function in order for it to work. Then, when a programmer wishes to use the double function, he would call it by name, as in this example:

      var y = double(3); //y = 6

    Functions and Object Oriented Programming

    • Object Oriented Programming (OOP) defines a paradigm in which programmers represent data as "objects," or discrete entities that store data and functions. The advantages of OOP are many, but a relevant one here is the fact that JavaScript, as an object oriented programming language, stores functions as objects. When a programmer defines a function, JavaScript saves it as an object in memory. Essentially, this means that a function defined by a programmer can also be assigned to a variable, the same way as a sentence, an integer, or a Boolean value.

    Examples of Assigning Functions to Variables

    • When a programmer defines a function, she can immediately assign the function to a variable. The variable will then behave as a function, taking arguments and returning values (depending on how the programmer defines the function). The following code is an example of assigning a function to a variable in JavaScript:

      var double = function doubleNum(x)

      {

      return 2 * x;

      }

      In this example, the variable "double" takes the code for the function. The function is the same as the "doubleNum" function.

    Using the Function as an Argument

    • Programmers can pass functions as arguments to other functions in the same way he passes other variables. For example, this code passes the "doubleNum" function to the "quadNum" function, which multiplies a number by two, and then by two again:

      function quadNum(x, doublefunction)

      {

      var double = doublefunction();

      return 2 * double(x);

      }

      quadNum(2, doubleNum);

      "quadNum" takes two arguments: the number x to multiply, and a function "doublefunction." Then, quadNum assigns doublefunction to the variable "double," and multiplies the result of double(x) and 2. To call quadNum, the programmer supplies a number and the doubleNum function.

Related Searches:

References

  • Photo Credit Stockbyte/Retrofile/Getty Images

Comments

Related Ads

Featured