Can some one explain what is the difference between these two code blocks? Why would we ever need the first type when second one is more concise.
First
var Utility;
(function (Utility) {
var Func = (function () {
function Func(param1, param2) {
var self = this;
this.Owner = param2;
}
return Func;
})();
Utility.Func = Func;
})(Utility || (Utility = {}));
Second:
var Utility;
(function (Utility) {
Utility.Func = function (param1, param2) {
var self = this;
this.Owner = param2;
}
})(Utility || (Utility = {}));
context: First version was code generated by typescript compiler and I am trying to understand why did it generate the first instead of a simpler second.
The only difference between the code blocks is that the first has a function scope around the code that creates the Func
function. The only reason to do that would be to create a scope where you can declare variables that would not be available in the outer scope:
var Utility;
(function (Utility) {
var Func = (function () {
var x; // a variable in the inner scope
function Func(param1, param2) {
var self = this;
this.Owner = param2;
}
return Func;
})();
// the variable x is not reachable from code here
Utility.Func = Func;
})(Utility || (Utility = {}));