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

3 comments:

kanchana said...

Everything is fine, am happy about your blog. Thanks admin for sharing the unique content, you have done a great job I appreciate your effort and I hope you will get more positive comments from the web users.
Java training in chennai

Suseela Susiee said...



That is very interesting; you are a very skilled blogger. I have shared your website in my social networks..!


Online Reputation Management

Abitha Chetna 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