JavaScript Calling a Function From a Function

JavaScript Calling a Function From a Function thumbnail
JavaScript uses functions as objects.

JavaScript is a scripting language for the Web used to perform calculations or control the layout of Web pages while embedded in HTML script. JavaScript's power and flexibility comes from the fact that JavaScript is an object-oriented language, meaning that it contains some functionality of other languages. Because of this, many types of data in JavaScript are objects, including functions. Functions behaving as objects allows programmers to do interesting things with them, like nest them in other functions.

  1. Functions and Return Values

    • Functions typically take a list of arguments, execute some code based on those arguments, and either perform an operation or return a value. When a function returns a value, this means that the function in a sense outputs a value, which a programmer can assign to a variable or use in another computation. The return value can be a scalar data type (such as an integer), or an object. For example, the following function takes a number and returns the number multiplied by three:

      function triple(x)

      {

      return x * 3;

      }

    Nested Functions

    • Inside a function, the programmer can define yet another function to perform some task. The definition process is exactly the same, but use of the function differs. When a programmer declares a function in global scope, she can use that function throughout the program. When a programmer declares a function within another function, however, the only place she can use that function is inside the function she declared it in. The following example illustrates this point:

      function triple(x)

      {

      function addThree(y){

      return y + y + y;

      }

      return addThree(x); //returns the return result of "addThree"

      }

      function double(x)

      {

      addThree(x); //illegal, addThree can only be called from inside the "triple" function

    Variables and Nested Functions

    • The reason functions cannot call any functions declared inside other functions is because the internal function is not in the outside functions "scope." The "addThree" function exists in the scope of the "triple" function. the "double" function is outside triple's scope, and cannot access its internal parts. However, "addThree" is inside "triple's" scope, and therefore can access the internals of "triple." This means that variables declared in "triple" are fair to use for "addThree," and the function could look like this:

      function triple(x)

      {

      function addThree(){

      return x + x+ x;

      }

      return addThree

    Returning Other Functions

    • Since functions are objects, a function can return a function rather than data. This means that certain functions can take data to give to other, nested functions, and return those functions. Then, the programmer can call the internal function from out of the external functions scope. The following example details how this works:

      function out(x) {

      function add(y)

      return x + y;

      }

      return inside;

      }

      x = out(3); //x now holds the function object "add(y)" which has an x value of 3

      y = x(2); // x = "add(2)" which is 3 + 2, so y = 5

Related Searches:

References

  • Photo Credit Ablestock.com/AbleStock.com/Getty Images

Comments

Related Ads

Featured