Mega Code Archive

 
Categories / JavaScript Tutorial / Object Oriented
 

Dynamic Prototype Method

This method uses a flag (_initialized) to determine if the prototype has been assigned any methods yet. The methods are only created and assigned once. function Car(sColor, iDoors) {     this.color = sColor;     this.doors = iDoors;     this.drivers = new Array("A", "B");     if (typeof Car._initialized == "undefined") {         Car.prototype.showColor = function () {             alert(this.color);         };         Car._initialized = true;     } }