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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Serge Kagiema - Fay](https://uploads.teamtreehouse.com/production/profile-photos/2749722/micro_me.jpg)
Serge Kagiema - Fay
2,879 PointsI 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](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThat 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?
![jason chan](https://uploads.teamtreehouse.com/production/profile-photos/3328982/micro_myself.jpg)
jason chan
31,009 PointsOkay 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.
![Serge Kagiema - Fay](https://uploads.teamtreehouse.com/production/profile-photos/2749722/micro_me.jpg)
Serge Kagiema - Fay
2,879 PointsThanks a lot Jason, I really appreciate your help. :)
![jason chan](https://uploads.teamtreehouse.com/production/profile-photos/3328982/micro_myself.jpg)
jason chan
31,009 PointsSerge Kagiema - Fay No problem man. I hope it helps.
Serge Kagiema - Fay
2,879 PointsSerge Kagiema - Fay
2,879 PointsThanks a lot, Steven!
Serge Kagiema - Fay
2,879 PointsSerge Kagiema - Fay
2,879 PointsThank very much for your answer. It made things clear to my head. I just have to apply them now! Again thank you very much!