Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript Closure should be renamed just IIFE, because Closure and IIFE are two separate things. #700

Closed
nonopolarity opened this issue Mar 26, 2016 · 5 comments

Comments

@nonopolarity
Copy link

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.

@MelissaMMDP
Copy link
Contributor

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/

@nonopolarity
Copy link
Author

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

@gkalpak
Copy link

gkalpak commented Mar 27, 2016

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).

@nonopolarity
Copy link
Author

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’s 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.

@johnpapa
Copy link
Owner

It should just say "Scope"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants