Sunday, April 05, 2015

Clouse that return Object literal and how to use returned Object literal Methods

Returning Object literal from Closure and using methods and Properties returned in the object literal.

 

 

var Inventory= function()

{

var names = ['Honda', 'BMW', 'Kia', 'Toyota']; // it will be initialized only once.

 

 // return an object literal so that we can access more than one operation

return {

     FindIfCarMakeIsInList : function(make)

     {

         return names.indexOf(make) > -1 ;

     },

 

     TotalMakesWeHave : names.length,

     AddMake : function(m)

     {

        if(names.indexOf(m) === -1)

        {

           names[names.length] = m;

        }

        return 1;

     }

   }

}();

 

alert('Is Acura in list ->' + Inventory.FindIfCarMakeIsInList('Acura'));

alert('How many car make we have ? ' + Inventory.TotalMakesWeHave);

alert('add Acura in list ->' + Inventory.AddMake('Acura'));

alert('Is Acura in list ->' + Inventory.FindIfCarMakeIsInList('Acura'));

alert('How many car make we have ? ' + Inventory.TotalMakesWeHave);

 

 

Thanks

Pradeep

No comments: