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
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

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:

var person = new Object( );
person.name = "surya";
person.branch = "cse";
document.write(person.name);

 

Another way for creating the above object is shown below:

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

 

Nested objects can be created as shown below:

person.address = new Object( );
person.address.dno = "Door Number";
person.address.street = "Street Name";

 

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:

document.write(person.name);

or

document.write(person[“name”]);

 

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

for(var  p  in  person)
{
    document.write("Property Name: ", p, "; Value: ", person[p], "<br />");
}

 

Constructors

 

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:

function person(p_id, p_name)
{
    this.id = p_id;
    this.name = p_name;
}

 

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:

function  display_person( )
{
    document.write("Person id is: ", this.id, "<br />");
    document.write("Person name is: ", this.name);
}

 

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( );

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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 *