Objects in JavaScript

In this post we will take a look on Objects in JavaScript. To start with almost everything in JavaScript is an Object. So understanding of Objects are very essential for JavaScript programming.

In JavaScript you can create a function in many ways

  1. By using Object Literals
  2. By using new operator
  3. By using Object.create()

Let us explore one by one various options to create object in JavaScript. Very simply you can create an Object using literals


var student = {
 name: "dj",
 grade: 9,

}

You can create empty object as following. We are creating an object with no properties as below,


var studentempty = {};

A complex object can be created as following. In below code studentinfo property is complex property and taking properties of Student object.


var sportStudent =
 {
 sports: "cricket",
 activeyear: 4,
 studentinfo :
 {
 name: student.name,
 grade : student.grade
 },
 coach : "Mr Singh"

}

 

Some important points about creating object using object literals are as following,

  1. Object literal is an expression
  2. It creates new object each time it appears in the code
  3. A single object literal can create many objects in loop

Now let us explore that how could we create objects using new operator.

 


var student = new Object();
var studentArray = new Array();

  1. new operator creates a new object
  2. new operator initialize created object also
  3. new operator invokes a function as give in above code snippet.

Function invoked after new operator is known as Constructor. For all native types JavaScript has predefined constructors.

image

Other way to create object is by using Object.create() method. You can create object using Object.create() as following ,


var student = Object.create(
 {
 name: "dj",
 grade : 10
 }
 );

Some important points about Object.create() is as following ,

  1. It is a static function
  2. To use it pass properties like we passed name and grade to create object

When you create object using Object.create() there are always two parameters to this

  1. Prototype
  2. Properties

We will discuss prototype in further posts. And second parameter property (in above code name and grade) we pass to create object.

In these ways you can create object in JavaScript. I hope you find this post useful. Thanks for reading.

5 thoughts on “Objects in JavaScript

  1. Pingback: Dew Drop – July 8, 2013 (#1,580) | Alvin Ashcraft's Morning Dew

  2. Pingback: Objects in JavaScript | Danny Acuna's Blog

  3. Pingback: How JavaScript Objects are Associate Array | debug mode......

  4. Pingback: JavaScript Objects and Inheritance using Kendo UI | Telerik Helper

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s