0

I am not very good in JavaScript. so when I saw a block of code now then many area is not clear. So someone please help me to understand.

I know this below way people declare their module

var Module = (function () {
  var privateMethod = function () {
    //A Private Method
    var privatemember; // scope is only private method
  };
  return {
    publicMethod: function () {
      //you can call private method here.
    }
  };
})();

Module.publicMethod(); //works

Just I saw another bit different code for module pattern as follows where knockout.js is used.

var HMS = HMS || {};

$(function () {

    HMS.PatientModel = function () {
        this.Patient_Name = ko.observable();
        this.Patient_Address = ko.observable();
    };

    HMS.PatientViewModel = function () {
        var patient = ko.observable(),
        loadPatient = function () {
            var newModel = new HMS.PatientModel();
            newModel.Patient_Name("Premkumar");
            patient(newModel);
        };
        return {
            patient: patient,
            loadPatient: loadPatient
        };
    } ();


    HMS.PatientViewModel.loadPatient();

    ko.applyBindings(HMS.PatientViewModel);

});

1) What is this code var HMS = HMS || {}; ?

2) See this $(function () {}) ();

Why module has no name specific. see my first code where I give a name to my module like this way var Module = (function () {}) ()

3) Inside module code every function name has started with HMS.............why like HMS.PatientModel = function () { };

Please help me to understand the second set code point wise. Thanks

5
  • curios to ask multiple question, but did you research for each individually? Commented Jul 3, 2015 at 19:53
  • i am not advance js developer. so the code which i highlighted here was confusing me. so i asked and looking for some one help. thanks Commented Jul 3, 2015 at 20:00
  • I'll give you a quick answer. If you're interested in research. Commented Jul 3, 2015 at 20:01
  • Are you familiar with JS logical values truthy and falsey? Do you know how JS variables scoped and what closures are? Commented Jul 3, 2015 at 20:02
  • if possible redirect me to right article from where i can acquire the knowledge of JS variables scoped & closures. thanks Commented Jul 3, 2015 at 20:08

2 Answers 2

1
var HMS = HMS || {}; 

that expression defines the var HMS to HMS or empty object if it is not defined is a short hand for

if(HMS) {
  var HMS = HMS;
} else {
  var HMS = {};
}

2) You are creating an object from an IIFE

They are declaring and empty object if it does not exist, and decorating it with the methods/functions once the function below its executed. is the same as this:

var HMS = {
   PatientModel : function () {},
   PatientViewModel : function () {}, 
}

3) And that is why they use HMS inside the function.

var HMS = {};
HMS.PatientModel = function() {};
HMS.PatientViewModel = function() {};

You should read about Closures, IIFE, and How to “properly” create a custom object in JavaScript?

Sample and short explanation of closure:

A closure is when you have access to variables that are not in the lexical scope of the function For example, a function declared inside another function, will have access to the parent variables.

eg:

(function(){
    var a = 1;

    function mainFunction() {

        function innerFunction() {
            var b = 2

            function subFunction() {
                console.log(a); //We have access to "a" here.
                console.log(b); //We have access to "b" here.
            }

           subFunction();
        }


        innerFunction();
        console.log(a); //We have access to "a" here.
        console.log(b); //We dont have access to "b" here. //error
    }
    mainFunction();

})();
console.log(a);  //We dont have access to "a" here. //error
Sign up to request clarification or add additional context in comments.

4 Comments

the code i posted related to module design pattern if yes then why they did not specify any name for his module ?
does the Closures mean when a main function will have inner function then inner function will be called Closures? am i right ?
I should post a closure example give me a minute.
I've updated the example but basically a closure is when you create a function inside a function, the inner function should have access to the parent scope variables. I recomend to you to read the linked "closures" link is better explained there.
1

1) what is this code var HMS = HMS || {}; ?

If HMS is undefined HMS is to be equal to an empty object otherwise use HMS as is (in your case HMS is an object).

2) see this $(function () {}) ();

It's called IIFE.

3) why like HMS.PatientModel = function () { };

HMS is an object and just adding its property with value. Value may be anything.

4) From your comment in another answer, why they didn't define module name?

They have defined the module name as Module. See var Module = (function(){}());

2 Comments

this is from var Module = (function(){}()); 1st set code and i am curious about second set code. 1st set code i understand. thanks
does the Closures mean when a main function will have inner function then inner function will be called Closures? am i right ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.