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
Post a Comment