Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

11/02/2015 Categories: Javascript. No Comments on User Defined Objects

Creation and Manipulation of User Defined Objects


An object is a real world entity that contains properties and behaviour. Properties are implemented as identifiers and behaviour is implemented using a set of methods.


An object in JavaScript doesn’t contain any predefined type. In JavaScript the new operator is used to create a blank object with no properties. A constructor is used to create and initialize properties in JavaScript.


Note: In Java new operator is used to create the object and its properties and a constructor is used to initialize the properties of the created object.


An object can be created as shown below:


var obj = new Object( );


The properties of an object can be accessed using the dot (.) operator. In JavaScript, the properties are not fixed as in Java. Number of properties can vary at runtime i.e., new properties can be added as and when needed.


In JavaScript properties are not variables, they are just names which are used to access values and hence are never declared. Creating and accessing properties is shown below:



Another way for creating the above object is shown below:


var  person = {name: “surya”, branch: “cse” };


Nested objects can be created as shown below:



The properties can be accessed with the dot operator or using the property name as an array subscript. For example let’s consider printing the name of the person object created above:






To step through all the properties of an object we can use the for-in loop as shown below:





A constructor is a special function which is used to create and initialize the properties of an object. A constructor has the same name as the object. A constructor can be called by using the new keyword.


A constructor can reference the properties of its object by using this keyword. A constructor can be defined as shown below:



Now, the above constructor person can be called by new keyword as shown below:


person1 = new person(101, “teja”);


If the object should also contain a method, it is initialized in the same way as a property is initialized. Let’s consider a method for displaying the details of the person object as shown below:



Add a property named display to the constructor as shown below:


this.display = display_person;


Now, we can access the method to print the details of a person object as shown below:


person1.display( );


Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up