Understand Object in JavaScript

Sourav.Kayal
Posted by in JavaScript category on for Beginner level | Points: 250 | Views : 6741 red flag

Understand Object in JavaScript

Understand Object in JavaScript

In this article we will learn the concept of object in JavaScript. It’s says that JavaScript is partially object oriented programming language. It means that JavaScript does not support full features of object oriented programming language like C++ ,C# or Java.

But the truth is that, everything (almost) in JavaScript is object. So, when we will code in JavaScript we can call each and every single token as object. In JavaScript below are the very popular object.

  • Date is object in JavaScript
  • Function is object in JavaSctipt.
  • Object is and objects in JavaScript

And many more like that. In this article we will see how to define object and set property on that. Try to understand below code.

Create object on fly
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <script>
            var obj = new Object();
            obj.name = "sourav";
            obj.surname = "kayal";
            alert("Name:- " +  obj.name + " Surname:- " + obj.surname);
 
        </script>
   
    </form>
</body>
</html>

In JavaScript object can create using “new” keyword, it’s very similar with other object oriented programming language. As JavaScript is loosely type checking language, we can assign an object into “var” variable.  Then we are setting property of that object. In traditional object oriented programming ,at first we create class and then we populate object according to that class. In JavaScript the scenario is little different. We can create object and set property on fly. Here is sample output of above program.


 Initialize object using constructor.

In our previous discussion, we said that in JavaScript function is also an object. In this example we will see how to initialize object using constructor. Have a look on below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 
</head>
<body>
    <form id="form1" runat="server">
        <script>
            function funScript(name, surname) {
                alert(name +" " + surname);
            }
            var value = new funScript("Sourav", "Kayal");
        </script>
    </form>
</body>
</html>

In this example we have declared a function called “funScript” and it takes two parameters. In next line we are creating one object of “funScript” function and in creating time we are passing two parameters into it. Here is sample output.


We can attach function to object.

We can attach any function to an object in JavaScript. In this example we will see how to do that? Have a look on below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     
</head>
<body>
    <form id="form1" runat="server">
        <script>
            var abc = new Object();
            abc = function ()
            {
                alert("abc function");
            }
            abc();
 
        </script>
    </form>
</body>
</html>

In this example, we have attached one function to a object and then we are calling to it. Here is sample output.


Conclusion:-

In this article we have seen how to define object in JavaScript. Hope you have understood the concept. In coming article we will learn more fundamental concepts of JavaScript.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)