Angular-styleguide: JavaScript Closure should be renamed just IIFE, because Closure and IIFE are two separate things.

Created on 26 Mar 2016  路  5Comments  路  Source: johnpapa/angular-styleguide

https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y010

It mentions JavaScript Closures and IIFE. But using IIFE to create a local scope to prevent leak out to the global space has nothing to do with closures.

In C language, there is no closure, but we can use the same or similar technique just like IIFE to create a local scope and create variables that won't leak to the global space. So closures and IIFE are different concepts. What is being used is IIFE. Closure, it doesn't really matter if it exist or not in this case. It will be better if we use and propagate the correct concept around.

All 5 comments

Maybe in C there is no closure but in JavaScript "An IIFE is often used to create scope to encapsulate modules. Within the module there is a private scope that is self-contained and safe from unwanted or accidental modification. This technique, called the module pattern, is a powerful example of using closures to manage scope".

http://www.sitepoint.com/demystifying-javascript-closures-callbacks-iifes/

that is incorrect concept. Isolating local variables inside of a local scope is IIFE and has nothing to do with a closure at all.

I (sort of) agree with @jianlin here. There is typically no closures involved in registering an Angular service/controller/filter/directive/etc inside an IIFE (as suggested in this guide).

An IIFE is often used to create scope to encapsulate modules

@MAustinMMDP, because it _"is ofter used"_, it doesn't mean it is used _every time_.
Angular has it's own module system, so (as far as I understand this styleguide) IIFEs are not used as a means to encapsulate modules, but as a way to isolate local variables (e.g. a function declarations) from the global context.

In that sense, the quote from http://www.sitepoint.com/demystifying-javascript-closures-callbacks-iifes/ is correct (in general) but irrelevant (in this particular case).

actually the line on SitePoint really should say something like:

An IIFE is often used to create scope to encapsulate modules. Within the module there is a private scope that is self-contained and safe from unwanted or accidental modification. This technique, called the module pattern, is a powerful example of using [local scope, or a function with local scope, or simply, a function] to manage scope, and it鈥檚 heavily used in many of the modern JavaScript libraries

what it means is:

the module pattern, is a powerful example of using a local scope to prevent leaking into the global scope.

It should just say "Scope"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philmerrell picture philmerrell  路  10Comments

annabellor picture annabellor  路  9Comments

jansepke picture jansepke  路  12Comments

bampakoa picture bampakoa  路  3Comments

Bekt picture Bekt  路  13Comments