3

Can someone please explain to me what the heck does this mean?

!function (global, moduleDefinition) {
  'use strict';

  var dependencies = [];

  if (typeof define === 'function' && define.amd) {
    define(dependencies, moduleDefinition);
  } else if (typeof exports === 'object') {
    module.exports = moduleDefinition.apply(null, dependencies);
  } else {
    global.Utilities = moduleDefinition.apply(null, dependencies);
  }

}(this, function () {
  'use strict';

  var Utilities = {};

  return Utilities;

});

This has been passed to me to follow as our new module.

Joe

Youssef
  • 190
  • 1
  • 3
  • 13
  • @Quentin where in this question is mentioned about exclamation mark? – Krzysztof Safjanowski Aug 04 '14 at 15:08
  • 1
    @KrzysztofSafjanowski — Very first character of code in the question. (Otherwise it is a "Close as unclear what you are asking" or "Too broad" because there are lots of different things that could be explained in that question. It really should have been written to have been more clearly focused in the first place). – Quentin Aug 04 '14 at 15:11
  • @Quentin – the title of question was changes – how about now? – Krzysztof Safjanowski Aug 04 '14 at 15:18
  • Sounds better, thanks. – Youssef Aug 04 '14 at 15:22
  • 1
    @KrzysztofSafjanowski — It would be better if the OP clarified what they meant rather than people answering it putting their interpretation there. – Quentin Aug 04 '14 at 15:22

2 Answers2

3
  • ! exclamation mark (or any other symbol lie (+) turns code to expression - you can invoke it immediately

Example

(function() {}());
!function() {}();

Wrong version

You can’t invoke function declaration!

function() {}() // wrong, don’t try to run this

Example

You can pass arguments to invoked function

(function(foo) {
    console.log(foo, foo === 3); //3, true foo is equal to passed value
}(3));

You can pass global object – in browser environment it this points to window object, in Node – it points to global object. As we want write one version of code without forking it for environment detection it is simpler to use global as variable that can hold pointer to global object. Compare window.setTimeout (for browser) vs this.setTimeout (for Node) vs global.setTimeout (for both)

(function(global) {
    global.setTimeout(function() {
        console.log('I’m running independent to browser or server environment');
    }, 1000);
}(this));

You have missed something for moduleDefinition, where is rest?

Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
  • This is great I got it now, one thing more, why do we pass global variables to the invoked function if the function already has access to them? what is the point of that? – Youssef Aug 04 '14 at 15:09
  • This simple function has acccess to **global this**, but it can be executed as method of some object. After that – `this` will points to the object itself instead of global. – Krzysztof Safjanowski Aug 04 '14 at 15:11
  • Thank you so much, well explained :) – Youssef Aug 04 '14 at 15:14
  • 2
    @user2668652: On a related note, the structure that allows for this kind of coding is called an Immediately-Invoked Function Expression (IIFE). It's a way for JS developers to isolate code, preventing it from leaking into the global scope. Greg Franko has a really good write-up on how this works and why it's useful: http://gregfranko.com/blog/i-love-my-iife/ – Blackcoat Aug 04 '14 at 15:24
-2

It means the code following this statement has to be run in strict mode. This can be used to make sure no mistakes are made in the code following this.

More information about strict mode: http://www.w3schools.com/js/js_strict.asp

Jerodev
  • 32,252
  • 11
  • 87
  • 108