I'm building a library that is a loading screen which allows me to call Loader.Show(), Loader.Hide() or Loader.Step() from various different documents.
I've built the library, however I do not fully understand the various ways to declare objects. Whats the difference in the following two models, and which is better?
//Example 1
var Robot = (function () {
var metal = "Steel";
return {
killAllHumans: function() {
alert(metal);
}
}
})();
Robot.killAllHumans();
//Example 2
var NonRobot = {
runState: "Normal",
run: function() {
alert(this.runState);
}
}
NonRobot.run();
I understand that both example 1 & create objects. I also understand that in Example 1, metal is a private variable. I do not know how to create a private variable in example 2. Is the only difference the scope?