4

I am new to JavaScript (coming from Java) and want to understand this function

(function (global, factory) {
if (typeof define === 'function' && define.amd) {
    define(['../numeral'], factory);
} else if (typeof module === 'object' && module.exports) {
    factory(require('../numeral'));
} else {
    factory(global.numeral);
}

}

It's from this github page https://github.com/adamwdraper/Numeral-js/blob/master/src/formats/currency.js

My question is: Why isn't there a name for the function that takes global and factory as parameter ?

What I would expect is something like this:

function myFunction(p1, p2) {
return p1 * p2;   // The function returns the product of p1 and p2
}

Here, we have the name myFunction but in the above example, we don't have a name for the function. Why? And why is that allowed in JavaScript?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Blnpwr
  • 1,793
  • 4
  • 22
  • 43
  • 2
    Welcome to JavaScript :) In this file, an anonymous function is declared. Then, it is being immediately invoked in place. Moreover, there is another anonymous function, which is passed as a second parameter `factory`. – Yeldar Kurmangaliyev Jun 05 '19 at 12:51
  • Possible duplicate of [What is the (function() { } )() construct in JavaScript?](https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript) – barbsan Jun 05 '19 at 13:01

3 Answers3

4

Yes. They are called anonymous functions in Javascript.

Usually these functions are supplied as callbacks to another function.

Or they could be immediately invoked as Jack said in his answer.

t.c
  • 623
  • 5
  • 19
1

It's because it's in an IIFE - you don't need to name the function, and the only real use of naming it would be recursion.

It's like this:

var func = function (global, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['../numeral'], factory);
    } else if (typeof module === 'object' && module.exports) {
        factory(require('../numeral'));
    } else {
        factory(global.numeral);
    }
}

You call it like:

(func)();

Or:

func();

It's a function expression - it doesn't have a name, it's assigned to a named variable. Unnamed functions are known as anonymous functions, and you use them as testing/callback functions.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • I see, so when I want to call the function how do I call it then? Let's say we are not using your version, but the version in the question. How can I call the function that has "no name" (so to speak) – Blnpwr Jun 05 '19 at 12:55
  • 2
    It's already being called @Blnpwr - the (this, ...) after the code you posted calls the function. To name it you'd have to declare it outside the function and assign it inside the function if you wanted to use the named function outside of itself. – Jack Bashford Jun 05 '19 at 12:56
  • Ah, I understand. And what happens when I have two of this function? So one more function that has no name. And I want to call both of them, do I need to use `this, this` ? – Blnpwr Jun 05 '19 at 12:59
  • Additionally: since it is an IIFE, as you say, this function is triggered/started automatically, is that right? – Blnpwr Jun 05 '19 at 13:05
  • 1
    Yes @Blnpwr - It means Immediately Invoked Function Expression. – Jack Bashford Jun 05 '19 at 13:05
1

Anonymous functions are just that, functions without names. They cannot be called by name like you would normally use a function. We actually use them a lot in javascript.

A common use for it would be for a function that takes another function as a parameter, like .sort(). Sort allows you to pass a custom function to handle the sorting logic, so with an anonymous function, it would like like this:

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
//[1, 2, 3, 4, 5]

See how the function is being passed as the only parameter to sort. An equivalent to this could also look like:

var numbers = [4, 2, 5, 1, 3];
var mySortFunction = function(a, b){
   return a - b;
}
numbers.sort(mySortFunction);
//[1, 2, 3, 4, 5]

Anonymous functions are just a clean and quick way to create functions that you would not reuse in your code.

SpeedOfRound
  • 1,210
  • 11
  • 26