I’m currently using the following pattern to create JS modules to be used. However, I can’t figure out if there is any difference, or any benefit, to doing it in the first style vs the second.
1st way
var UI = UI || {};
(function () {
var BtnShape = function (x, y, width, height, size, color, text)
{
this.initialise(x, y, width, height, size, color, text);
}
var p = BtnShape.prototype;
p.isEnabled = false; //<--------------
p.initialise = function (x, y, width, height, size, color, text)
{}
UI.BtnShape = BtnShape;
})();
2nd way
var UI = UI || {};
(function () {
var BtnShape = function (x, y, width, height, size, color, text)
{
this.initialise(x, y, width, height, size, color, text);
}
var p = BtnShape.prototype;
p.initialise = function (x, y, width, height, size, color, text)
{
this.isEnabled = false; //<---------------
}
UI.BtnShape = BtnShape;
})();
The only difference that I can see here is the order in which
isEnabledproperty is set. By nesting theisEnabledproperty into the initialise function, you’d need to run theinitaliseprocedure before theisEnabledhas any value. I’d assume that you would run the initialise function before doing anything, but if you don’t thenisEnabledwould be empty.