Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace vs self-invoking functions [closed]

Tags:

javascript

I have a question: What´s the better approach to limit the scope in Javascript: Using a namespace like this:

var NAMESPACE = {};
NAMESPACE.foo = function() { 
   console.log('Hello');
}
NAMESPACE.foo();

or should I use the self-invoking function like this

(function() {
    function foo() { console.log('Hello'); }
    foo();
})();

Is it always good to have a namespace or can I omit it if I just use one big self-invoking function where I put all my stuff?

like image 747
Keen Avatar asked Sep 07 '12 08:09

Keen


People also ask

What is a self-invoking function?

A self-invoking (also called self-executing) function is a nameless (anonymous) function that is invoked immediately after its definition. An anonymous function is enclosed inside a set of parentheses followed by another set of parentheses () , which does the execution. (function(){ console.

What is the use of self-invoking function in JavaScript?

Self-Invoking Functions A self-invoking expression is invoked (started) automatically, without being called. Function expressions will execute automatically if the expression is followed by (). You cannot self-invoke a function declaration.

Can you explain IIFE with code?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog.


1 Answers

Both of your code samples have very different applications.

NAMESPACE.foo = function(){} and (function(){})() are both function expressions and (just as function declarations) delimit the inner scope and have access to vars/functions in their outer scopes.

The main difference is, with the namespace you can call the function anywhere that is in the same scope as the namespace, while the IIFE will only be executed when encountered.

There is no "best practice" without context, so I'll give some examples:

  • If your function expression is supposed to only execute once, an IIFE will do the job very well while a namespace will take some memory until it is garbage collected.
  • If your function expression will run multiple times (e.g. inside a for loop), an IIFE will cause a performance loss (and is not recommended by JSLint, or anyone in fact). It's better to have a defined function object which you call multiple times than creating a new function object for each iteration.
  • Ultimately, it depends on your user case. IIFEs are executed when encountered (e.g. Program/Function Body, IF Block etc.), while a namespace will store the function object and allow you to call it whenever you need, provided it is accessible in the given scope.

Reading material

  • Immediately-Invoked Function Expression (IIFE) - Excellent read for understanding IIFEs.
  • How do JavaScript closures work?
  • Named function expressions demystified - Covers a good paragraph about Function Expression vs Declaration. Also covers browser-specific behavior.
like image 140
Fabrício Matté Avatar answered Sep 20 '22 16:09

Fabrício Matté