javascript - Can anyone tell the correct way of forming this code and using it -


var hash = {           func1: function(){       },      return {                  contains: function(key) {         return keys[key] === true;               },         add: function(key) {              if (keys[key] !== true){              keys[key] = true;                       }      }       }  

can explain me how return works here... can call hash.contains(key) feel code have written wrong... mean structure?

var hash = (function () {     var keys = {};     return {         contains: function (key) {             return keys[key] === true;         },         add: function (key) {             if (keys[key] !== true) {                 keys[key] = true;             }         }     } })(); 

that think trying achieve. code thought trying create hash object add , contains method checking if keys exisist within keys var making protected variable.

what above code create closure contains keys variable (an object) , returns object 2 methods contains , add.

you can read more closures here http://jibbering.com/faq/notes/closures/. closures 1 of biggest reasons javascript flexible is.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -