I'm quite new to TypeScript, and I'll try to learn JavaScript by it. Since I'm a .NET programmer, I can create the classes in TypeScript and then let the TypeScript compiler magically transform it to JavaScript, and I would like to use the JavaScript generated files after all. This will help me in learning JavaScript.
Now, I've a simple class, used to perform validation on method arguments. The typescript implementation looks like:
class Guard {
static ThrowIfNull(argument, argumentName) {
if (argument == null) { throw "The argument '" + argumentName + "' cannot be null."; }
}
}
On the TypeScript playground, this produces the following JavaScript:
var Guard = (function () {
function Guard() {
}
Guard.ThrowIfNull = function (argument, argumentName) {
if (argument == null) {
throw "The argument '" + argumentName + "' cannot be null.";
}
};
return Guard;
})();
So, now I do want to use this piece of JavaScript code, not TypeScript code into my web application.
I've created a jsFiddle to make it more easy to understand.
Now, I've read somewhere that in your JavaScript, you can better wrap everything inside a function
which is like this:
(function() {
'use strict';
-- Code goes here.
})();
See this JsFiddle for updated code. But, now when I do run my application, I get a 'Guard is undefined error.' Can someone explain to me why this is happening?
I assume I must remove the (function()) {})(); then, but that means I misunderstood the concept at first, so what's the purpose of wrapping everything inside such a function?
Edit If I remove the function wrapper, then JShint is producing an error that says me that I should use the form function of 'use strict'.
Thanks for helping me out.