It's vital to point out that I've got very limited historical experience in javascript. I'm looking for the possible reasoning behind the concepts used in the code examples I've provided below. The application uses a combination of MVC, JavaScript, jQuery, knockout.js and Kendo UI.
The first bit of code looks like this:
lx.vm = (function ($, ko, kendo, undefined) {
"use strict";
var HCA = function () {
var Compliance = {
complianceId: ko.observable("DEN-AB")
};
debugComplianceId = function () {
return alert("debugComplianceId method has been called!");
};
ko.applyBindings(Compliance);
},
hc = new HCA();
return
{
hc: hc
};
}(jQuery, ko, kendo));
This code is contained in a .js
file referenced in the scripts section of my .cshtml
file. What I'm lost on is the first line, lx.vm =
. What is achieved by doing this? I understand the concept of an IIFE, so the code contained within the function is at least understandable, but I'm unsure of the prefix to begin this file. Is it simply adding the function to the global scope, while keeping all members of the function private? Or is there something else going on here?
My next question pertains to this bit of code:
hc = new HCA();
return
{
hc: hc
};
Again, no var
to explicitly declare a variable, and I don't understand the reasoning in aliasing HCA and returning the alias. Why alias it at all and not just return HCA?
The most closely related answer I've been able to find for my question is here: How do I namespace JavaScript code with or without IIFEs?
However, there really isn't an explanation included to back the answer by csharpfolk.
Clarification: The JavaScript presented executes successfully. What I'm looking for is confirmation, backed by an explanation, of what is achieved with the first line of code, as well as the second example in the code.