0

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.

EverToffee
  • 1
  • 1
  • 2
  • 6
    [Javascript function fails to return object when there is a line-break between the return statement and the object?](https://stackoverflow.com/q/18221963/4642212) – Sebastian Simon Jun 23 '17 at 19:44
  • 2
    Hmm, good point actually- the code as presented actually returns nothing(`undefined`)! Good catch. OP, you have my sympathy for your current work situation, if nothing else. – Karl Reid Jun 23 '17 at 19:45
  • the whole code is, well not really useful. – Jonas Wilms Jun 23 '17 at 19:45
  • 1
    [Automatic Semicolon Insertion](https://stackoverflow.com/q/2846283/215552). – Heretic Monkey Jun 23 '17 at 19:47
  • 3
    The answer to this question is most likely something along the lines of "the author is an engineer who thinks very highly of his cleverness". – Heretic Monkey Jun 23 '17 at 19:50
  • 1
    It looks like [Revealing module pattern](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript). Those aliases allow you to export renamed functions but here it looks like this pattern was applied blindly. – Przemysław Zalewski Jun 23 '17 at 19:51
  • @Jonasw nor was it intended to be. The question was not about the contained code, but the pattern in which the function was being used and implemented. – EverToffee Jun 23 '17 at 21:46
  • @PrzemysławZalewski I agree the code does seem to mimic Revealing module pattern. Unfortunately that doesn't explain the way in which it's been written and what is achieved being written such a way. Now I've got a better baseline to work off of and store in my brain. Thanks. – EverToffee Jun 23 '17 at 21:47

0 Answers0