stuart-d2
1/14/2015 - 3:28 PM

MODULES js : Prototype Pattern

MODULES js : Prototype Pattern

/*
Pattern seen prior, creates a constructor function and added a prototype to it. 
Each object constructed from the constructor will act as a module -- The constructor in the Prototype pattern is spinning off modules, that encapsulates its functionality 

PROS
	• Uses js built in feature.. This is what prototype was made for
	• Functions an properties arent part of the global scope, thereby ensuring privacy
	• Loaded into memory only once
	• You can ovverride the parent MONO -- in specific instances 

CONS
	• Handling the THIS keyword can be tricky
	• You define the prototype separately from the constructor function, whereas in other languages its all done in the constructor.  A little more work.  


*/

/*Basic Template */
function ObjectName(<<put here parameters>>) { 
	//create instance functionality
	}
	ObjectName.prototype = { 
		// create prototype functionality
	}

/* Example */
  var Car = function (type) {
	this.speed = 0;  
	this.type = type || "no type";  
	
};
	Car.prototype = {
		drive: function(newSpeed) { 
			this.speed = newSpeed;
			}
} 

// Later in code
car car = new Car("bmw"); 
car.drive(60);