Prototype property of JavaScript Object

Prototype property

Objects in JavaScript are based on prototypes. A prototype is a property of an object that serves as the base of another object, defining and implementing members that a new object must have. Prototype property objects are shared amongst all instances of a given object type, and so all instances also share the prototype object’s members.

An object is tied to its prototype by an internal property. While creating a new instance of a built-in type, such as Object or Array, these instances automatically have an instance of Object as their prototype.

Consequently, objects can have two types of members: instance members (also called “own” members) and prototype members. Instance members exist directly on the object instance itself, whereas prototype members are inherited from the object prototype. The prototype members can be created using prototype property of the object. Consider the following example:

Prototype_Chaining

//Example 1

//Creating the EventHandler class which is having one instance member eventName

var EventHandler =function(eventName){

this.eventName= eventName;

};

//Prototype property: Defining the addEvent method

EventHandler.prototype.addEvent=function(eventObj){

// …     //do something

};

// Prototype property: Defining the deleteEvent method

EventHandler.prototype.deleteEvent=function(eventName){

//…     //do something

};

// Creating onClickEventHandler object from EventHandler

var onClickEventHandler= new EventHandler(‘onClick’);

//Adding instance property to onClickEventHandler which is accessible to onClickEventHandler only

onClickEventHandler.clickType=’mouseClick’;

// Creating onSubmitEventHandler object from EventHandler

var onSubmitEventHandler= new EventHandler(‘onSubmit’);

// Adding instance property to onSubmitEventHandler which is accessible to onSubmitEventHandler only

onSubmitEventHandler.submitType=’formSubmit’;

When executing the above piece of code, you will get the following object notation:

prototype_obj

 

 

 

 

 

 

 

 

 

 

Figure 1: Prototype Object Notation

Each object has it’s own instance members eventName (instance member from Eventhandler class). JavaScript provide the capability to add and delete any member in object at any time. So each object also have its own property clickType for onClicEventHandler and submitType for onSubmitEventHandler.

Both the objects have one (same) copy of prototype (__proto__) object (having method addEvent and deleteEvent). Any method added using prototype property is being added inside the prototype object.

If you notice in above object structure, the prototype object is also having internal prototype (__proto__) member that is the default Object and inherited by every sub object this behavior is known as “Prototype Chaining”.

Will explain in detail about “Prototype Chaining” in next blog.

About these ads

About sunnyratra
I am a young technology evangelist and a total java freak, I also enjoy exploring and learning java script and jQuery plugins. I am serving one of the best Software firm in the World. Major of my experience comes under Java & J2EE. I am having a good hands on Portlet Development(JSR 286), Spring 2.x and 3.0, Spring AOP, Hibernate 3.0, Maven, Coherence, JQuery, Prototype and JavaScript UI Frameworks. I feel that I know so little but I guess enough to have an opinion! So this blog is for sharing my view on stuff, and ideas which at least I believe are worthwhile. Happy Reading! Suggestions and feedbacks are always welcome.

2 Responses to Prototype property of JavaScript Object

  1. lista says:

    as always an excellent posting. the way you write is awesome. thanks. adding more information will be more useful.

  2. Adnil Young says:

    Great article. Really explained to me how Javascript works.

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: