var hash = {
func1: function(){
},
return {
contains: function(key) {
return keys[key] === true;
},
add: function(key) {
if (keys[key] !== true){
keys[key] = true;
}
}
}
Can anyone explain me how the return works here…
Can i call hash.contains(key)
I feel the code what i have written is wrong… mean the structure?
That is what I think you were trying to achieve. From your code I thought you were trying to create a hash object with a add and contains method for checking if keys exisist within your keys var but also making it a protected variable.
What the above code does is create a closure that contains the keys variable (an object) and returns an object with the two methods contains and add.
You can read more about closures here http://jibbering.com/faq/notes/closures/. Closures are one of the biggest reasons Javascript is as flexible as it is.