0

Recently I have been learning JavaScript programming. I got some trouble while dealing with a JavaScript module (based on functions, which are the only things in JavaScript that create a new scope).

1.

var getter = function() {
  var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
  ];
  return function(number) {
    return dayNames[number];
  };
};
console.log(getter(2));

2.

var getter = function() {
  var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
  ];
  return function(number) {
    return dayNames[number];
  };
}();
console.log(getter(2));

I'm quite confused about the different result caused by the omission of the () parentheses.

Thanks for your help.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Closing parentheses denote an [**Immediately-Invoked Function Expression**](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression) (or IIFE). – Obsidian Age Jul 27 '17 at 02:52
  • Although many might say this is pedantic, `()` are called "parentheses", whereas `{}` are called "braces". This established convention makes communication about the syntactical elements easier. <-- added prior to edits. – Fred Gandt Jul 27 '17 at 02:53
  • Thank you all !!!! – TommyHUI Jul 27 '17 at 03:00

2 Answers2

0

I understand your confusion. The first two general cases you see are probably relatively clear-- () at the end of function defines a function that takes no parameters (same for a named function like function myFunction(). And then it is used to call functions with no parameters as well, such as in:

function myFunction() { // Defines the function named myFunction
    console.log('hello world');
}
myFunction(); // calls myFunction, and will console log 'hello world')

The extra case that is confusing is the Immediately Invoked Function Expression (IIFE). This is when we define a function and then immediately invoke it. This can be useful for a few reason-- often for purposes of creating closures:

var counterizer = (function () {
 var count = 0;
 return {
  incrementCount: function () {
   count++;
   console.log(count);
  }
 }
})(); // the inner anonymous function is immediately invoked

counterizer.incrementCount();
counterizer.incrementCount();

Here the inner anonymous function is immediately invoked and returns an object that increments count and console logs it-- it is stored in a closure, and inaccessible to the outside world.

Hope this helps!

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
0

your function return a function which means the () must be added if it just return number or string you doesnt have to add () for example

//this function just return function getter()(number)
var getter = function() {
 var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
 ];
 //this one return function(number)
 return function(number) {
  return dayNames[number];
 };
};
//so if you want function to return function you must add () after that function like this:

console.log(getter()(2))