Take that you want a single JavaScript object that offers some methods, like this:
var a = {
c: "hello",
talk: function(){
console.log(this.c);
}
}
But you don't want people to be able to access c directly, you want it to be sort of private. I've figured out this solution, but I don't know if there are any better ones:
var a = new function(){
var c = "hello";
this.talk = function(){
console.log(c);
};
}
Can you think of a better solution? And can you tell why it is better?
Could you point any (performance, mantainability, or other) trouble this approach can give me?
I leave here an extra example in which I think this could be conceptually usefull:
var Animation = {
from_right: "translate(100%,0)",
from_bottom: "translate(0,100%)",
zoom: "translate(0,0) scale(0)",
default: "translate(100%,0)",
get_initial: function(type){
switch (type) {
case "from_right":
case "from_bottom":
case "zoom":
return this[type];
break;
default:
return this["default"];
break;
}
},
get_final: function(){
return "translate(0,0) scale(1)";
}
}
I know this works perfectly like this but, for me, conceptually, it is much clearer when using or reviewing it to have those first config properties as private variables. For example, if I log Animation and find that it has 2 methods, I realise faster how to use it than if all those properties that are not supposed to be accessed are printed as well. It is not about how to do something, it is about how to do it clearer and better.
Thanks!