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...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.