I used Javascript closure to protect variables by making them local to return functions, for example:
closure = function() { var secretVar = 'Secret'; return { "msg" : function() { console.log(secretVar); } }; }(); console.log(closure.secretVar);
I feel like I have a pretty good idea of this, giving me the ability to control access to internal variables, if at all possible.
Now i am facing this problem
closure = function() { var secretVar = ['Secret One','Secret Two']; return { "del" : function(modMe) { modMe = secretVar; modMe.slice(1,1); console.log(modMe); }(secretVar), "secretVar" : function() { console.log(secretVar); } }; }(); closure.del();
I want shut.del () to return Secret One, but I want secretVar to remain untouched, but that is not the case. The del () function modifies the link, not the copy, and I'm not sure how to make it copy secretVar and change this.
I guess it will be in shape
(function(y) {
but I could not get this to work. Any ideas?
source share