Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Prototype works?

Status
Not open for further replies.

Tarbuza

Technical User
Dec 13, 2000
56
US
Hi,

I would appreciate if anyone could help me by explaining the concept of prototype in JavaScript with examples. I have read about the prototype property but I am still not clear. Thanks.
 
Prototype defines a property that is shared by all objects of the specified type.

Syntax:

objectType.prototype.propertyName = value

Parameters:

objectType - the name of a constructor or function specifying an object type.

propertyName - the name of the property to be created.

value - the property value initially assigned for all objects of the specified objectType.

Prototype can be applied to any object created with new operator - user-defined objects or standard javascript objects like Array, Date, String, Number, etc.

Here's an example from original Netscape's javascript documentstion:

var today = new Date()
var birthday = new Date(95,12,17)
Date.prototype.description=null
today.description="Oh what a beautiful mornin\'"
birthday.description="The day you were born"

After you set a property for the prototype, all subsequent objects created with Date() will have the property:

startDate=new Date()
startDate.description="Started the daily grind"

Hope it will help you.
good luck
 
Thanks a million. It is now crystal clear.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top