Sunday, April 05, 2015

Close with function notation

See how private and public methods are accessible from outside. Last statement will not work,  tell me why ?

 

var Inventory = function InventoryConstructor(n)

{

     var that = function(n)

     {

        title = n;

        GetTitle = function()

        {

             return title;

        };     

     };

 

     var names = ['Honda', 'BMW', 'Kia', 'Toyota'];

     that.FindIfCarMakeIsInList = function(m)

     {

         return names.indexOf(m) > -1 ;

     };

 

     that.TotalMakesWeHave = names.length;

    

     that.AddMake = function(m)

     {

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

       {

          names[names.length] = m;

       }

       return true;

     }

 

     return that;

}('India inv');

 

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);

alert(Get Title ' + Inventory.GetTitle()); // This will fail, tell me why ?

 

 

Thanks

Pradeep

1 comment:

Unknown said...

Thanks for sharing these information.Bigdata is very nice topic.We are providing online training classes.This is just the kind of information.

Hadoop Training in Chennai