Object in JavaScript

JavaScript is an object oriented language, but JavaScript does not use classes. JavaScript is prototype based, not class based. In JavaScript objects play a very important role. With the help of object oriented programing we can use

1. Encapsulation – A language mechanism for restricting access to some of the object’s components.
2. Abstraction – abstraction is the process of separating ideas from specific instances of those ideas at work.
3. Inheritance – inheritance is when an object or class is based on another object or class, using the same implementation, it is a mechanism for code reuse.
4. Polymorphism – polymorphism is the provision of a single interface to entities of different types.

Objects are composed of attributes. If an attribute contains a function, it is considered to be a method of the object otherwise , the attribute is considered a property. A property is an association between a name and a value. Object properties are usually variables that are used internally in the object’s methods. In JavaScript almost everything is an object. Even primitive data types (except null and undefined) can be treated as objects. In addition, JavaScript allows you to define your own objects.
Creating objects:

object name=new Object();

This is a basic syntax for creating objects in JavaScript. But it does not ends here. There are much to discuss than meets the eye. We can add properties, methods in objects. we can create custom objects. We can do many things with objects. We will get down to it step by step.
Another approach for creating an object is a particular method called Object.create(). With this method we are able to create an object with multiple parameters.
Creating custom object:

function person(parameter){     //defining an object function.

var myobject=new person("Hi!") //myobject is now an object of type person!

In the above code snippet we have defined an object function. And after that it has been instantiated by a type called “person”. The object function is used to define and declare an object. The parameter is optional, and with it, allows us to pass in values to an object. To use this object, all we have to do is instantiate it, by using the keyword “new”.”myobject” is now an object, an instance of “person”.

User-defined object:
All user-defined objects and built-in objects are descendants of an object called ‘Object’. The “new” operator is used to create an instance of an object. To create an object, the new operator is followed by the constructor method.

var obj = new Object();
var person = new Array("Mike", "David");
var day = new Date("March 14, 2014");

A constructor is a function that creates and initializes an object. JavaScript provides a special constructor function called Object() to build the object. The return value of the Object() constructor is assigned to a variable. The variable contains a reference to the new object.

var book = new Object();   // Create the object
    book.subject = "JavaScript"; // Assign properties to the object
    book.author  = "Zakas";
    document.write("Book name is : " + book.subject + "");
   document.write("Book author is : " + book.author + "");

Now we have an object and it’s instance. But it won’t give us any result until we insert some method or properties inside the object.

In the following section we will discuss about that.
Accessing methods:

The methods are functions that let the object do something or let something be done to it. There is little difference between a function and a method, except that a function is a standalone unit of statements and a method is attached to an object and can be referenced by the this keyword.

document.write("This is test");

To add methods we need to first declare and define a function for each method, and then associate this function with the object function.
The first step to adding methods is to implement the method functions.

//first method function
function area(){
  var calarea=this.radius*this.radius*3.14
  return area
//second method function
function caldiameter(){
  var diameter=this.radius*2
  return diameter

function circle(r){

In the above code snippet we can see that in first and second method function we have defined the process to calculate area and diameter of a circle. In second phase from an object we have called those methods to get our desired result. But still we can’t use these methods. To use these methods, we have to instantiate the object, and access the methods.

var mycircle=new circle(20)

Accessing properties:
Object properties can be any of the three primitive data types, or any of the abstract data types, such as another object. To add properties to a user defined object, directly embed the properties into the object function, with each property preceded by the keyword “this” plus dot (.).

function object(parameter){
  this.secondproperty="This is the second property"

The with keyword is used as a kind of shorthand for referencing an object’s properties or methods. The properties and methods for the object can be used without naming the object.

with (object){
    properties used without the object name and dot

If you find this article helpful, you can connect us in Google+ and Twitter.

Enter your email address:Delivered by FeedBurner

Leave a Reply

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