Voice of a Developer: Part Ten - JavaScript Functions

Introduction

 
JavaScript is the language of the 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    
 
code
 
The () invokes the function,
 
function
 

Properties of function

 
Let us dive into some properties of the function, ex-
 
function
 
  • Call v/s Apply

     
    If you want to assign the 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 
     
    The call requires the parameters be listed explicitly Apply 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 a scenario, it is recommended to attach a property with a function. It is acting like a static property to keep track of the 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 sometimes we don’t pass the correct number of arguments. Therefore, it is good practice to check the 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  
    6. function by using property length  
    7.   
    8. function myFunction(a, b) {  
    9.     var argsRequired = arguments.length;  
    10.     var argsActual = arguments.callee.length;  
    11.     if(argsRequired != argsActual) {  
    12.         throw new Error("Wrong # of args passed");  
    13.     }  
    14.     return a * b;  
     
    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