Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I really get confused with the following meanings: closure, scope, context & IIFE for namespacing

I'm trying to understand how javascript works with the namespacing using the immediately-invoked function expression (IIFE) and I came I cross with those terms and I really got confused.

I don't even don't how to apply those things to my programming. Even the IIFE seems really confusing.

for instance

//code

(function(window){

var foo, bar;

function private(){
    // do something
}

})(this);

//End: code

Why does it take two parameters? (Window and this) How does it work?

Please help me out.

3 Answers

Steven Parker
Steven Parker
229,921 Points

That function is only taking one parameter, window. But it is being declared and called at the same time, and the parameter that is passed to it in the call is this (which refers to the current window).

When a function is declared and invoked at the same time, it is often referred to as a self-invoked (or self-executing) anonymous function or more correctly as an immediately-invoked function expression (IIFE, pronounced "iffy").

The latter term is considered more correct because IIFEs don't actually execute themselves, they are invoked like any other function. And they don't have to be anonymous, they just often are.

The scope of the variables foo and bar is limited to the IIFE, including the internal function private. They cannot be accessed by other code.

A closure would be created if the IIFE returned a function (such as private) and that return was stored in a variable. Then, that function could be invoked using the variable at a later time, still having access to the scope-limited variables which no other code could access.

Does that help clear things up?

Thanks a lot, Steven!

Thank very much for your answer. It made things clear to my head. I just have to apply them now! Again thank you very much!

jason chan
jason chan
31,009 Points

Okay lemme take a whack at this?

iife is immediate invoked function. It invokes itself. But it's function within a function. The reason we use IIFE to closure "wrap a function in functions" is to avoid global pollution. Global pollution is when the same name of functions and variables get over written. We do not want that.

For scope inside function has access to variables wrapped within the closure. But outside functions do not have access to wrap enclosed variables.

crockford has notes on it.

http://javascript.crockford.com/private.html

http://www.w3schools.com/js/js_function_closures.asp

Thanks a lot Jason, I really appreciate your help. :)

jason chan
jason chan
31,009 Points

Serge Kagiema - Fay No problem man. I hope it helps.