Javascript Practice Day 6 : Immediately invoked function expression and its difference with normal funciton
Immediately Invoked function Expression and difference between Construction pattern of object creation
Immediately Invoke function Expressions are useful in increassing the performance of the program as the global scope is placed under the local scope .The main difference between IIFE and other form of object creation is that with 'others' we can create different instance of the objects
but with IIFE we can't , it can be called multiple times.But IIFE remembers the values/operation it went through and next time performs on the modified value which is not present in case of normal function
//IIFE//
var counter=(function()
{
var i=1;
return {
get: function(){
return i;
},
set: function(a){
i=a;
},
add: function(){
i=i+1;
}
}
})();
//IIFE//
//As each time a the same instance is called we do see a constant increase of value
console.log(counter.get());
counter.set(5);
counter.add();
console.log(counter.get());
console.log(counter.get());
counter.add();
console.log(counter.get());
Normal function
var counter_normal=function()
{
var i=1;
return i+1;
}
console.log(counter_normal());
console.log(counter_normal());
console.log(counter_normal());
//Normal function
Normal function returning object and creating instance without 'new'
var counter2 =function(){
var i=1;
return {
get: function(){
return i;
},
set: function(a){
i=a;
},
add:function(){
i=i+1;
}
}
}
var obj1=counter2();
console.log(obj1.get());
obj1.set(5);
obj1.add();
console.log(obj1.get());
//As each time a different instance is called we do not see a constant increase of value
var obj1_=counter2();
console.log(obj1_.get());
obj1_.add();
console.log(obj1_.get());
Constructor function and creating instance with new operator
var counter3=function(){
this.i=1;
this.get =function(){
return this.i;
}
this.set =function(a){
this.i=a;
}
this.add =function(){
this.i=this.i+1;
}
}
var obj2=new counter3();
console.log(obj2.get());
obj2.set(5);
obj2.add();
console.log(obj2.get());
Immediately Invoke function Expressions are useful in increassing the performance of the program as the global scope is placed under the local scope .The main difference between IIFE and other form of object creation is that with 'others' we can create different instance of the objects
but with IIFE we can't , it can be called multiple times.But IIFE remembers the values/operation it went through and next time performs on the modified value which is not present in case of normal function
//IIFE//
var counter=(function()
{
var i=1;
return {
get: function(){
return i;
},
set: function(a){
i=a;
},
add: function(){
i=i+1;
}
}
})();
//IIFE//
//As each time a the same instance is called we do see a constant increase of value
console.log(counter.get());
counter.set(5);
counter.add();
console.log(counter.get());
console.log(counter.get());
counter.add();
console.log(counter.get());
Normal function
var counter_normal=function()
{
var i=1;
return i+1;
}
console.log(counter_normal());
console.log(counter_normal());
console.log(counter_normal());
//Normal function
Normal function returning object and creating instance without 'new'
var counter2 =function(){
var i=1;
return {
get: function(){
return i;
},
set: function(a){
i=a;
},
add:function(){
i=i+1;
}
}
}
var obj1=counter2();
console.log(obj1.get());
obj1.set(5);
obj1.add();
console.log(obj1.get());
//As each time a different instance is called we do not see a constant increase of value
var obj1_=counter2();
console.log(obj1_.get());
obj1_.add();
console.log(obj1_.get());
Constructor function and creating instance with new operator
var counter3=function(){
this.i=1;
this.get =function(){
return this.i;
}
this.set =function(a){
this.i=a;
}
this.add =function(){
this.i=this.i+1;
}
}
var obj2=new counter3();
console.log(obj2.get());
obj2.set(5);
obj2.add();
console.log(obj2.get());
Comments
Post a Comment