Advanced JavaScript: Play With Object in JavaScript

Introduction

 
This is the second presentation of the Advanced JavaScript article series. As the name indicates, this series provides somewhat deeper concepts of JavaScript. The previous article provided the history of JavaScript and the importance of JavaScript in modern web applications. You can read it here

Advance JavaScript  

 
In this article, we will understand the concept of objects in JavaScript. The first concept is that JavaScript is an interpreted language and executes in the user's browser. In each and every browser (that is compatible with JavaScript) the parser is available and it's executing the code. If there is any confusion in that, I suggest you run the Date() function in JavaScript and it will always show the system date, even if it is wrong.
 

Objects in JavaScript

 
Nearly everything in JavaScript is an object. Even primitive datatypes (except null and undefined) can be treated as an object. Here are some examples:
  • Dates are objects in JavaScript
  • Arrays and strings are always objects
  • Math and regular expressions are always objects
  • The most interesting fact is that functions are also objects in JavaScript
Ok, so the concept is that everything (almost) in JavaScript is an object or at least they behave like an object. We all now have the concept of objects in Object-Oriented Programming.  They have properties and methods. The syntax to access a property of an object is:
 
objectName.propertyName
 
The string is an object.
 
Let's create a simple object. As we have explained earlier, a string is also an object in JavaScript. We will use one example of a string.
  1. <head id="Head1" runat="server">    
  2.     <title></title>    
  3.     <script>    
  4.             function TestScript() {    
  5.             var val = "JavaScript";    
  6.             alert("Length of String is:- " + val.length);    
  7.         }    
  8.     </script>    
  9. </head>    
  10. <body onload="TestScript();">    
  11.     <form id="form1" runat="server">    
  12.     <div>    
  13.     </div>    
  14.     </form>    
  15. </body>    
  16. </html>   
    In this example, we are declaring a string (that is an object) and accessing the length property of the string object. It shows the number of characters in a string.
     
    String Example
     

    Few more objects in JavaScript

     
    Here is a few more examples of JavaScript. As we said, in JavaScript, Date, Array and Math are all objects.
    1. <script>    
    2. function TestScript() {    
    3. //Create Date Object    
    4. var date = new Date();    
    5. //Create Array Object    
    6. var array = new Array("Sourav""Kayal");    
    7. //Math object has random() function    
    8. var data = Math.random();    
    9. }    
    10. </script>   

      Create object and set property on the fly

       
      In this example we will create an object on the fly and we will set few properties on that. This object creation is very similar to object creation in high-level programming languages. The difference is that it does not require a class definition.
      1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>    
      2. <!DOCTYPE html>    
      3. <html xmlns="http://www.w3.org/1999/xhtml">    
      4. <head id="Head1" runat="server">    
      5.     <title></title>    
      6.     <script>    
      7.             function TestScript() {    
      8.             var author = new Object();    
      9.             author.name = "Sourav Kayal";    
      10.             author.interest = "JavaScript";    
      11.             author.publisher = "cshrpcorner.com";    
      12.             alert("Name :- " + author.name + "Interest:- " + author.interest + "Publisher:- " + author.publisher);    
      13.         }    
      14.     </script>    
      15. </head>    
      16. <body onload="TestScript();">    
      17.     <form id="form1" runat="server">    
      18.     <div>    
      19.     </div>    
      20.     </form>    
      21. </body>    
      22. </html> 
      Sample output
       
      Create Object
       

      Set property of object using the constrictor

       
      In the example above we saw that, to create an object on the fly, if we want to set a property of the object when it's created then we need to implement a constructor. Try to understand the following example.
      1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>    
      2. <!DOCTYPE html>     
      3. <html xmlns="http://www.w3.org/1999/xhtml">    
      4. <head id="Head1" runat="server">    
      5.     <title></title>    
      6.     <script>    
      7.             function person(name, surname) {    
      8.             this.name = name;    
      9.             this.surname = surname;    
      10.         }    
      11.         function TestScript() {    
      12.             var p = new person("Sourav""Kayal");    
      13.             alert("Name:- " + p.name + " Surname:- " + p.surname);    
      14.         }     
      15.     </script>    
      16. </head>    
      17. <body onload="TestScript();">    
      18.     <form id="form1" runat="server">    
      19.     <div>    
      20.     </div>    
      21.     </form>    
      22. </body>    
      23. </html>   
      Here the person function behaves like a constructor and it takes the name and surname as an argument. We can even implement constructor overloading in this context. We are using the "this" operator to identify the current object. This is the output.
       
      property of Object
       
      A function is an object.
       
      As we said earlier, even a function in JavaScript is an object and it's called a function object. The following function definition is very similar with a function definition in the C family of languages.
      1. <html xmlns="http://www.w3.org/1999/xhtml">    
      2. <head id="Head1" runat="server">    
      3.     <title></title>    
      4.     <script>    
      5.             function Addition(num1, num2) {    
      6.             this.num1 = num1;    
      7.             this.num2 = num2;    
      8.             alert(num1 + num2);    
      9.         }    
      10.         function TestScript() {    
      11.             var value = new Addition(10, 20);    
      12.         }    
      13.     </script>    
      14. </head>    
      15. <body onload="TestScript();">    
      16.     <form id="form1" runat="server">    
      17.     <div>    
      18.     </div>    
      19.     </form>    
      20. </body>    
      21. </html>   
      Output
       
      Function object
       

      Another style to define a function.

       
      This is another style to define a function and the result is absolutely the same in both (the above style and this style).
      1. <script>    
      2.     var Addition = function (num1, num2) {    
      3.     this.num1 = num1;    
      4.     this.num2 = num2;    
      5.     alert(eval(num1) + eval(num2));    
      6. }    
      7.  function TestScript() {    
      8.     var value = new Addition(10, 20);    
      9.    }    
      10. </script>   
      What the difference is
       
      An obvious question in people's minds is, what is the difference between the two syntaxes? Let's try to understand them.
      1. function hello() {    
      2. }    
      3.  var hello = function () {    
      The first one is a regularly named function and the second one is a normal variable declaration with an anonymous function attached to it. Since this article is all about objects, we will not delve deeper into this topic. I am planning to present one dedicated article on this with a detailed explanation including an example.
       

      Property of the object is also an object

       
      Let's see how to attach a function object to the property of another object. Here a person is one object and in the next line, we are assigning an anonymous function to the name property of the person object. Hence the name property of the person object is behaving as a function object.
      1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>    
      2. <!DOCTYPE html>    
      3. <html xmlns="http://www.w3.org/1999/xhtml">    
      4. <head id="Head1" runat="server">    
      5.     <title></title>    
      6.     <script>    
      7.                var person = {};    
      8.         person.name = function (name1) {    
      9.             alert("I am " + name1);    
      10.         }    
      11.         function TestScript() {    
      12.             person.name("sourav kayal");    
      13.         }    
      14.     </script>    
      15. </head>    
      16. <body onload="TestScript();">    
      17.     <form id="form1" runat="server">    
      18.     <div>    
      19.     </div>    
      20.     </form>    
      21. </body>  
      22. </html>   
      Here is the output of the code above.
       
      Property of object
       

      Conclusion

       
      In this article, we have understood the concept of objects in JavaScript. I hope you have understood them. In a future series, we will explain various interesting topics of JavaScript.
       


      Similar Articles