I need to access a private variable from an anonymous function. This anonymous function is set by the following:
coolObject = new (function(){
this.public = "public";
var private = "secrets";
// General functions here, no getter or setter for private
})();
I can easily read and write to coolObject.public
by doing console.log(coolObject.public)
or coolObject.public = "newValue"
, but how can I do the same to the private variable? Another thing is that I cannot add code to the constructor, coolObject
will always be initially defined like this.
So, to sum everything up, is there a way I can access a private variable from a anonymous function in JavaScript, and if so, how?
Edit: I have tried creating getters and setters by doing coolObject.getPrivate = function(){return private;};
, but that doesn't work.