Various Styles For Declaring Functions in JavaScript

Introduction
 
 In this article we will learn various styles for declaring and using functions in JavaScript applications. As we know, JavaScript is an Object Oriented Programming language and everything in JavaScript is an object. So, a function is also one type of object in JavaScript.
 A function is written as a code block (inside curly { } braces), preceded by the function keyword.
 
Let's see how to define a function. 
 
Open the Notepad++ editor and write the following code:
 
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4.        
  5. </head>  
  6. <body>  
  7.     <form id="form1" runat="server">  
  8.         <script>  
  9.             function samplefun() {  
  10.                 alert("Sample Function call");  
  11.             }  
  12.             samplefun();  
  13.         </script>  
  14.     </form>  
  15.     </form>  
  16. </body>  
  17. </html>  
 
 
Let us attach a function to a variable as in the following:
 
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4.        
  5. </head>  
  6. <body>  
  7.      <form id="form1" runat="server">  
  8.         <script>  
  9.               
  10.             var val = function samplefun() {  
  11.                 alert("This is another function");  
  12.             }  
  13.             val();  
  14.    
  15.         </script>  
  16.     </form>  
  17. </body>  
  18. </html>  
In this style of definition we created the function and attached it to a variable (val) . Now, using the variable we can access this function. We see that we are calling the function using val() and the function is being executed.
 
 
 
 
Now let us use a function like a class.
 
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4.        
  5. </head>  
  6. <body>  
  7.     <form id="form1" runat="server">  
  8.         <script>  
  9.               
  10.             function student() {  
  11.                  this.name = "Rama";  
  12.                  this.surname = "Sagar";  
  13.                  this.printInfo = function(){  
  14.                     alert("Name:- " + this.name + "Surname:- " + this.surname);  
  15.                 }  
  16.             }  
  17.             var p = new student();  
  18.             p.printInfo();  
  19.               
  20.    
  21.         </script>  
  22.     </form>  
  23. </body>  
  24. </html>  
In the preceding example we implemented a class type using the function. Here the student class (read function) contains two properties and one method. The properties are name and surname and the method is printInfo. We see that the method was created on the fly (dynamically). In the next line we are creating an object of the “student” class using the new operator and then we are calling the printInfo() function using an object of the “student” function or class.
 
 
 
 
 
Summary

In this article we learned how to define a function in JavaScript. In a future article we will learn more basic concepts of JavaScript.
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all