• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to use prototype in javascript?

sunilmkt

Member
As we know Prototypes allow us to easily define methods to all instances of a particular object and method is applied to the prototype, so it is only stored in the memory once. we can define prototype in many ways.

So I want to ask which methods is more suitable if we define prototype in header or any other place.
Does prototype defining methods depends upon our need & requirement?.
 
Prototype hasn't been updated for over a year. If possible, give jQuery a try. It does the same stuff and more, but it's easier and more common.
 
You should use prototypes only when you wish to declare a "non-static" method of the object.

Code:
var Test= function () {  };  
Test.prototype.getx = function (){   alert("x"); };  
Test.gety = function (){   alert("y"); }; 
 Test.gety();  // Correct  
Test.getx();  // Incorrect  
var Jay= new Test(); 
Jay.getx();  // Correct
 
You should use prototypes only when you wish to declare a "non-static" method of the object.

Code:
var Test= function () {  };  
Test.prototype.getx = function (){   alert("x"); };  
Test.gety = function (){   alert("y"); }; 
 Test.gety();  // Correct  
Test.getx();  // Incorrect  
var Jay= new Test(); 
Jay.getx();  // Correct

Pretty much this.

A good majority of the JS I work with is object oriented, so I use prototypes quite a bit.
 
Back
Top