0

i came across following example in my training guide for HTML5/javascript/css

I don't understand why the private variables from the method are added as private variables in underlying object/namespace ns.

(function () {
   this.myApp = this.myApp || {};
   var ns = this.myApp;
   var vehicleCount = 5;
   var vehicles = new Array();
   ns.Car = function () { }
   ns.Truck = function () { }
   var repair = {
   description: 'changed spark plugs',
   cost: 100
   };
}());

this is the explanation the book gives:

An IIFE (pronounced iffy) is an anonymous function expression that has a set of parentheses at the end of it, which indicates that you want to execute the function. The anonymous function expression is wrapped in parentheses to tell the JavaScript interpreter that the function isn’t only being defined; it’s also being executed when the file is loaded. In this IIFE, the first line creates the myApp namespace if it doesn’t already exist, which represents the singleton object that is used as the namespace. Next, an ns variable (for namespace) is created as an alias to the namespace to save typing within the IIFE, so ns can be used in place of this.myApp. After that, the private members of the namespace are defined by using the var keyword. Car and Truck are public, so they are prefixed with ns.

I would expect following code if they wanted to make these properties privates of the myApp 'namespace'

(function () {
   this.myApp = this.myApp || 
      {
         var ns = this.myApp;
         var vehicleCount = 5;
         var vehicles = new Array();
         var repair = {
            description: 'changed spark plugs',
            cost: 100
            };
      };
   ns.Car = function () { }
   ns.Truck = function () { }
}());
5
  • The given code doesn't even work in strict-mode. So, to be honest, get another book/guide. Commented Jul 10, 2014 at 14:28
  • possible duplicate of JavaScript module pattern with example Commented Jul 10, 2014 at 14:30
  • 1
    Lots of SO information on this topic. Some reference adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html Commented Jul 10, 2014 at 14:31
  • got this from an official MS book 70-480 HTML5/javascipt/css. Just don't get how those var's could belng to the ns object/namespace Commented Jul 10, 2014 at 14:35
  • @Upperstage thank you, your link made it clear what is going on :) Commented Jul 10, 2014 at 14:58

1 Answer 1

1

They are private because they are defined in the closure and totally encapsulated as local variables inside the closure. ns becomes a local variable too. There is no reference back to scope outside the closure.

This is a self calling closure and the scope of the definitions is similar to defining local variables in a "normal" function.

Sign up to request clarification or add additional context in comments.

Comments

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.