JS Design Pattern

by Samar Pattanayak

JavaScript

console.log("******* Design Pattern ********");
console.log("*****************REVEALING Modular Approach");
var objM= function(){
	var meth1= function(){
		console.log("****output return METH1");
	}
	var meth2 = function(){
		console.log("****output return METH2");
	}
	return {
		first : meth1,
		second : meth2
	}
}
var tM= new objM();
tM.first();// i can access meth1 through public variable;
try{
	tM.meth1();
}catch(error){
	console.log("try gain",error.name)
}
//it will not work
console.log("*******************MODULAR Approach");
var objB = function(){
	this.name = "Samar Pattanayak";
	this.address ="Balasore";
	this.printName = function(){
		console.log("Name Here...", this.name);
	}
	this.printAge = function(){
		consle.log("Age Here...", this.age);
	}
}
var tO= new objB();
tO.printName();

console.log("********************Singleton Pattern");
var objS= function(){
	var instance ;
	var methSingle = function(){
		console.log("From Singleton");
	}
	return {
		single : function(){
			if(!instance){
				instance =  methSingle();
			}
			return instance;
		}
	}
	
}
var tS= new objS();
tS.single();
console.log("************************Prototype Pattern");
var objP = function(){
	this.prop1 = "Property 1";
	this.prop2 = "Property 2";
	this.methProp = function(){
		console.log("Prototype Method 1");
	}
	this.meth2Prop = function(){
		console.log("Prototype Method 2");
	}
}

objP.prototype.proto1 = function(){
		console.log("Prototype Inherited Method");
}
objP.prototype.methProp = function(){
	console.log("checking from where its called")
}
var tP= new objP();
console.log(tP);
console.log(Object.getPrototypeOf(tP))//Same with below
console.log(objP.prototype);//same
tP.proto1();
tP.methProp();//this method is present in both instance and prototype but it will called from instance

console.log(tP.isPrototypeOf(objP))
//The isPrototypeOf() method allows you to check whether or not an object exists within another object's prototype chain.

//Errro...