Understand JavaScript Objects

Introduction

 
In this article, we will learn about objects in JavaScript. Everything in JavaScript is an object. In addition, JavaScript allows us to define our own objects. So, when we code in JavaScript we can call each and every single token an object. 
  • Arrays are always objects, even functions are always objects
  • Dates are always objects 
  • Booleans can be objects or primitive data treated as objects
  • Numbers can be objects or primitive data treated as objects
  • Strings are also objects or primitive data treated as objects
  • Maths and RegularExpressions are always objects
Let's see how to define an object and set a property on that.
 
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.     <title></title>    
  5. </head>    
  6. <body>    
  7.     <form id="form1" runat="server">    
  8.         <script>    
  9.             var obj = new Object();    
  10.             obj.name = "csharp";    
  11.             obj.lastname = "corner";    
  12.             alert("Name:- " +  obj.name + " Lastname:- " + obj.lastname);    
  13.      
  14.         </script>    
  15.         
  16.     </form>    
  17. </body>    
  18. </html>   
In JavaScript, we can create an object and set a property on fly. Here is a sample output of the program above. 
 
 
Now let's see how to initialize the object using constructor. Have a look at 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 samplefunction(name, lastname) {    
  10.                 alert(name +" " +    
  11. lastname);    
  12.             }    
  13.             var value = new samplefunction("Csharp""corner");    
  14.         </script>    
  15.     </form>    
  16. </body>    
  17. </html>   
In the code above we declared a function called “samplefunction” and takes two parameters. In the next line we are creating one object of the “samplefunction” function and at the time of creation, we are passing two parameters into it. Here is the sample output.
 
 
 
Now let's attach a function to an object in JavaScript. In the following example, we will see how to do that. Have a look at 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.                
  10.             var xyz = new Object();    
  11.             xyz = function ()    
  12.             {    
  13.                 alert("xyz function");    
  14.             }    
  15.             xyz();    
  16.      
  17.         </script>    
  18.     </form>    
  19. </body>    
  20. </html>   
 
 

Summary

 
In this article, we learned how to define an object in JavaScript. In a future article, we will learn some more basic concepts of JavaScript.