articles

Home / DeveloperSection / Articles / Java Script Objects

Java Script Objects

Danish Khan 4018 18-Oct-2012
Introduction:

In this article I am going to explain what objects in Javascript are and how to work with objects.Javascript is an Object Oriented Programming Language. Object Oriented Programming Language contains the four basic characteristics:

1.       Encapsulation: Encapsulation deals with storing the information (data and methods) which are related to each other together in one object.

2.       Aggregation: It is the capability to place one object in other object.

3.       Polymorphism: In polymorphism one function can have much form.

4.       Inheritance: Inheritance helps classes to inherit the functionality of other class.

Almost everything in Javascript is Object, whether string, Array, Number, Functions but Javascript allows us to create our own object. What is object?

Object is a special kind of data which contains some properties and attributes.

What are Object Properties?

Object Properties are the values which are associated with an object. The syntax for accessing the object is given below:

Syntax:   ObjName.propertyName

Through this example I am going to show you how we can access property of an object:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <script type="text/javascript">
        var message = "First example on object";
        var x = message.length;
        document.write("Length of message is: " + x);
    </script>
</head>
<body>
</body>
</html>
Output is:

Java Script Objects

User-Defined Objects:

User can create your own object in Javascript. With the help of new operator we can create their own object. To create our own object we have to write new keyword followed by the constructor method.  

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title></title>
    <script type="text/javascript">
        var book = new Object();
        book.name = "Programming with Javascript";
        book.price = 300;
    </script>
</head>
<body>
    <script type="text/javascript">
        document.write("Book name is: " + book.name + "<br>");
        document.write("Book price is: " + book.price + "<br>");
    </script>
</body>
</html> 
Output of the above code is:

Java Script Objects

Now I will show you how to assign method to an object:   

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title></title>
    <script type="text/javascript">
        function priceinfo(price) {
            this.price = price;
        }
        function book(booktitle) {
            this.title = booktitle;
            this.price = priceinfo;
        }
    </script>
</head>
<body>
    <script type="text/javascript">
        var mybook = new book("C# Programming");
        mybook.price = 200;
        document.write("Book title is : " + mybook.title + "<br>");
        document.write("Book price is : " + mybook.price + "<br>");
    </script>
</body>
</html>
 Output:

Java Script Objects

The with Keyword:

The with keyword is used to specify an object to which all of the unreferenced properties and methods in a following block of code are to be applied.

Syntax:
with(object){

Properties which are to be used

}

Program to depict the use of with operator

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
    <title></title>
    <script type="text/javascript">
        function priceinfo(price) {
            with (this) {
                price = price;
            }
        }
        function book(booktitle) {
            this.title = booktitle;
            this.price = priceinfo;

  }
    </script>
</head>
<body>
    <script type="text/javascript">
        var mybook = new book("C# Programming");
        mybook.price = 200;
        document.write("Book title is : " + mybook.title + "<br>");
        document.write("Book price is : " + mybook.price + "<br>");
    </script>
</body>
</html>  
Output:

Java Script Objects


Updated 07-Sep-2019

Leave Comment

Comments

Liked By