Javascript Practice Day 4 : Factory,Constructor,Dynmic Prototype and Prototype Pattern
//Reference : https://www.youtube.com/watch?v=xizFJHKHdHw&list=PL7pEw9n3GkoW5bYOhVAtmJlak3ZK7SaDf&index=1
// factory ,constructor prototype pattern
//in factory pattern we create a simple function with parameters which will return an object and thereby
//behaves a factory .
var peopleFactor = function( age, name,state){
var temp={}; ///creating a empty temp object
temp.age= age; //attaching the properties to the object
temp.name=name;
temp.state=state;
temp.printPerson=function(){
console.log(this.name+" , "+this.age+ " , "+this.state);
};
return temp; //returning the object
};
//creating differernt object from the factory function
var person= peopleFactor('John',23,'CA');
var person1= peopleFactor('Kim',23,'SC');
var person2= peopleFactor('lun',22,'TX');
//printing the different objects created from the factory
person1.printPerson();
person2.printPerson();
//Constructor Pattern - In constructor patteren we are attaching the properties to the function itself ,as functions are objects
//in javascript.Constructor pattern do not create an object so we have to create objects with new keyword. The problem with constructor pattern //is that all the object will have the properties which any be redundant , So we have the prototype pattern.
//since in Javascript function are object we are associating peoperties to the function itself
var peopleConstructor = function(age,name,state){
this.name=name;
this.age=age;
this.state=state;
this.printPerson =function(){
console.log(this.name+" , "+this.age+" , "+this.state);
}
}
//We are creating different objects on the function
var person4= new peopleConstructor('per4',23,'CA');
var person5= new peopleConstructor('per5',23,'CA');
person4.printPerson();
person5.printPerson();
//Prototype Pattern -- created an empty object and added properties with prototype to it , these will create shared propeties among the objects
//declaring the prototype
var peopleProto = function(){
};
//default properties associated to the prototype properties
peopleProto.prototype.name="";
peopleProto.prototype.age=0;
peopleProto.prototype.state="";
peopleProto.prototype.printPerson = function(){
console.log(this.age+" , "+this.name+" , "+this.state);
}
//create a prototype objects
var per6= new peopleProto();
//associating the properties to object
per6.age=23;
per6.name="Per6";
per6.state="CA";
console.log("From Prototype");
per6.printPerson();
console.log("name" in per6);
//dynamic prototype pattern
var peopleDynamicProto = function(name, age ,state){
this.name=name;
this.age=age;
this.state=state;
//if the funciton is not present then create the function in the prototype space or shared space
//when the first object is created since no printfunction is available create the fuction in protype space
// when the second object is created since printfunction is already available no function is created
if(typeof this.printPerson !== 'function'){
peopleDynamicProto.prototype.printPerson = function(){
console.log(this.age+" , "+this.name+" , "+this.state);
}
}
};
console.log("From Dynamic prototype");
var per7= new peopleDynamicProto("per7",23,"CA");
per6.printPerson();
// factory ,constructor prototype pattern
//in factory pattern we create a simple function with parameters which will return an object and thereby
//behaves a factory .
var peopleFactor = function( age, name,state){
var temp={}; ///creating a empty temp object
temp.age= age; //attaching the properties to the object
temp.name=name;
temp.state=state;
temp.printPerson=function(){
console.log(this.name+" , "+this.age+ " , "+this.state);
};
return temp; //returning the object
};
//creating differernt object from the factory function
var person= peopleFactor('John',23,'CA');
var person1= peopleFactor('Kim',23,'SC');
var person2= peopleFactor('lun',22,'TX');
//printing the different objects created from the factory
person1.printPerson();
person2.printPerson();
//Constructor Pattern - In constructor patteren we are attaching the properties to the function itself ,as functions are objects
//in javascript.Constructor pattern do not create an object so we have to create objects with new keyword. The problem with constructor pattern //is that all the object will have the properties which any be redundant , So we have the prototype pattern.
//since in Javascript function are object we are associating peoperties to the function itself
var peopleConstructor = function(age,name,state){
this.name=name;
this.age=age;
this.state=state;
this.printPerson =function(){
console.log(this.name+" , "+this.age+" , "+this.state);
}
}
//We are creating different objects on the function
var person4= new peopleConstructor('per4',23,'CA');
var person5= new peopleConstructor('per5',23,'CA');
person4.printPerson();
person5.printPerson();
//Prototype Pattern -- created an empty object and added properties with prototype to it , these will create shared propeties among the objects
//declaring the prototype
var peopleProto = function(){
};
//default properties associated to the prototype properties
peopleProto.prototype.name="";
peopleProto.prototype.age=0;
peopleProto.prototype.state="";
peopleProto.prototype.printPerson = function(){
console.log(this.age+" , "+this.name+" , "+this.state);
}
//create a prototype objects
var per6= new peopleProto();
//associating the properties to object
per6.age=23;
per6.name="Per6";
per6.state="CA";
console.log("From Prototype");
per6.printPerson();
console.log("name" in per6);
//dynamic prototype pattern
var peopleDynamicProto = function(name, age ,state){
this.name=name;
this.age=age;
this.state=state;
//if the funciton is not present then create the function in the prototype space or shared space
//when the first object is created since no printfunction is available create the fuction in protype space
// when the second object is created since printfunction is already available no function is created
if(typeof this.printPerson !== 'function'){
peopleDynamicProto.prototype.printPerson = function(){
console.log(this.age+" , "+this.name+" , "+this.state);
}
}
};
console.log("From Dynamic prototype");
var per7= new peopleDynamicProto("per7",23,"CA");
per6.printPerson();
Comments
Post a Comment