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 () { }
}());
JMan
  • 2,611
  • 3
  • 30
  • 51
  • The given code doesn't even work in [strict-mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode). So, to be honest, get another book/guide. – Yoshi Jul 10 '14 at 14:28
  • possible duplicate of [JavaScript module pattern with example](http://stackoverflow.com/questions/17776940/javascript-module-pattern-with-example) – Upperstage Jul 10 '14 at 14:30
  • 1
    Lots of SO information on this topic. Some reference http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html – Upperstage Jul 10 '14 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 – JMan Jul 10 '14 at 14:35
  • @Upperstage thank you, your link made it clear what is going on :) – JMan Jul 10 '14 at 14:58

1 Answers1

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.

TGH
  • 38,769
  • 12
  • 102
  • 135