| 
									 
										This chapter excerpt from 
										Microsoft AJAX Library Essentials by Cristian Darie, Bogdan 
									Brinzarea, is printed with permission from Packt 
										Publishing, Copyright 2007.
											| 
													Book Excerpt: Object-Oriented JavaScript
 |  
 |  
 
 
										
											| 
													Prototypes 
													You learned earlier that in JavaScript you should defi ne "class methods" 
													outside the body of the "class", in order to prevent their multiplication for 
													each instantiated object. Prototyping is a JavaScript language feature that 
													allows attaching functions and properties to the "blueprint" of a function. 
													When functions are added to a class (function) prototype, they are not 
													replicated for each object of the class (function). This reflects quite well 
													the behavior of classes in C#, although the core mechanism and the specific 
													implementation details differ greatly. A few facts that you should keep in mind 
													about prototypes are:
 |  
									
									Every JavaScript function has a property named prototype. Adding members to the 
									function's prototype is implemented by adding them to the prototype property of 
									the function.
									
									Private variables of a function aren't accessible through functions added to 
									its prototype.
									
									You can add members to a function's prototype at any time, but this won't 
									affect objects that were already created. It will affect only any new ones.
									
									You can add members to a function's prototype only after the function itself 
									has been defined.
									
										The Table "class" from the previous example contains a "method" named 
										getCellCount(). The following code creates the same class, but this time adding 
										getCellCount() to its prototype:
									 //Table classfunction Table (rows, columns)
 {
 // save parameter values to class properties
 this.rows = rows;
 this.columns = columns;
 }
 
 //Table.getCellCount returns the number of table cells
 Table.prototype.getCellCount = function()
 {
 return this.rows * this.columns;
 };
 Related LinksTest your Oops knowledge with 
											our multiple choice questions!
 Object Oriented Programming Interview Questions
 |