Voice of a Developer: Part Ten - JavaScript Functions

JavaScript is language of Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.

Before moving further let us look at the previous articles of the series:

In JavaScript, functions are the 1st class citizens of language. These are the bread and butter of JavaScript. Go in deep now:

Functions in-depth

Function keyword, followed by a name, followed by parentheses (). You can get function definition or invoke the function, ex:

  1. function myFunction(a, b) {  
  2. return a * b; // Function returns the multiplication of a and b  
  3. }  
code
The () invokes the function,

function

Properties of function

Let us dive into some properties of function, ex-

function

 

  • Call v/s Apply:

    If you want to assign value of a function we use straight forward way, i.e.,
    1. var val = myFunction (2,3); // val = 6  
    There are two other way to achieve it using call or apply property,
    1. var val = 0;  
    2. myFunction.apply(val,[2,3]); // 6 as output  
    3. myFunction.call(val,2,3); // 6 as output  
     Call requires the parameters be listed explicitlyApply lets you invoke the function with arguments as an array. 
     
  • The "constructor" property

    Every object has a built-in property named constructor. It refers to the function which created it. Let us understand in detail:
    1. function myFunction(a, b) {  
    2.     return a * b;                // Function returns the multiplication of a and b  
    3. }  
    4. var m = new myFunction();  
    5. console.log( m.constructor == myFunction); // true  
    6. console.log(m.__proto__.constructor == myFunction); // true, the other way to check is  
  • Defining Your Own Function Properties

    Many times we need a unique number say GUID or identifies which increments with each call made to function. In such scenario, it is recommended to attach a property with a function. It is acting like a static property to keep track of last value returned.
    1. function myFunction(a, b) {  
    2.     ++myFunction.p1;  
    3.     return a * b;                // Function returns the multiplication of a and b  
    4. }  
    5. myFunction.p1 = 1;  
    6. myFunction();  
    7. myFunction.p1 ; // will print 2  
  • Length property

    In a function, we pass arguments to process input and sometime we don’t pass correct number of arguments. Therefore, it is good practice to check required number of arguments needed for a particular function.
    1. function myFunction(a, b) {      
    2.     return a * b;  
    3. }   
    4. myFunction(2); // will give NaN because b is undefined  
    5. Its time to fix above function by using property length  
    6. function myFunction(a, b) {      
    7.     var argsRequired = arguments.length;  
    8.     var argsActual = arguments.callee.length;  
    9.     if (argsRequired!=argsActual) {  
    10.         throw new Error("Wrong # of args passed");  
    11.     }  
    12.     return a * b;  
    13. }  
    function

Summary

There are other useful properties also which you can look into detail. I hope you like this series. Please share your comments / feedback.

Read more articles on JavaScript: