Prototype object of javascript

Cp_raj
Posted by in JavaScript category on for Beginner level | Points: 150 | Views : 6037 red flag

Prototype is used to define classes or objects in JavaScript. Classes or objects are building blocks of Object Oriented Programming.

Introduction


The prototype object of javascript introduced in Javascript 1.1. In Visual Studio, you can get intellisense or its properties and methods.


Use of prototype

Prototype prebuilt simplifies the process of adding custom properties/methods to all instances of an object.  Keep the below code snippet in the html or aspx page and run it, you will notice that you will get a alert message.

Example:

<script language="javascript">


// create the prototype

function Car(cName, cModel) {

this.carName = cName;

this.carModel = cModel;

}

 

// declare another property

Car.prototype.price = 200000;

 

// now create a GetCarDetails function that will show the details

Car.prototype.GetCarDetails = function () {

return "Car Name:" + this.carName + " Car Model:" + this.carModel + " Car Price: " + this.price;

}

 

// now create the object of Car prototype

var oCar = new Car("Maruti Alto", "2009");

 

// Call the function that we created

alert(oCar.GetCarDetails() + oCar.Engine);

 

/******************

Car.prototype={

Price: 200000,

GetCarDetails = function ()

{

return "Car Name:"+this.carName+" Car Model:"+this.carModel+" Car Price: "+this.price;

}

}

******************/

</script>


Conclusion

Prototype offers   better performance in some browser(Firefox), and they provide better support for reflection, Intellisense and debugging.

Page copy protected against web site content infringement by Copyscape

About the Author

Cp_raj
Full Name: Rajkumar Pauldurai
Member Level: Starter
Member Status: Member
Member Since: 5/26/2009 6:29:52 AM
Country:

http://www.dotnetfunda.com
I started my career with Asp,ms-access,sql server 7,xml.Then i updated to .net,share point server,Basics about Biztalk server

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)