0

I have a basic understanding of IIFEs and protected/private/static values. As I'm learning about JS, I notice IIFEs being mentioned to keep variables and functions "protected." I also notice the same thing for PHP classes, like setting a property to protected or private.

But why would any of this be done in a real situation? Wouldn't NOT ACCESSING the variables or NOT ACCESSING the class properties have the same effect?

Bonus, also why use let and const for ES6 scoping, when you just don't have to access the variable?

  • 1
    One uses the appropriate tools for the task – Jaromanda X Aug 05 '17 at 00:18
  • Note, no problem statement appears at OP. See [Is it possible to delete a variable declared using const?](https://stackoverflow.com/questions/42424019/is-it-possible-to-delete-a-variable-declared-using-const), [Prevent JavaScript closure from inheriting scope](https://stackoverflow.com/questions/45260308/prevent-javascript-closure-from-inheriting-scope) – guest271314 Aug 05 '17 at 01:09

2 Answers2

0

IIFEs are anonymous functions, which are executed immediately after they're created (unless it's inside of another block of code which needs to wait to be called). They're useful for executing code without polluting the global namespace. If you want to complete a task once, and then immediately throw away the variables created while doing said task, IIFEs should be your go to.

The const keyword is very useful for declaring a variable that is not allowed to change. For example: const pi = 3.14159;

matthewninja
  • 360
  • 5
  • 21
  • So if I'm using a JS function just once, and never again, an IIFE would be appropriate? –  Aug 05 '17 at 00:48
  • @00Saad Yes, exactly. Essentially, you want to use an IIFE when it's a function that you're calling for the side effect rather than to get any value out of it. Also, it prevents clutter of the name space, leaving good variable names open. – matthewninja Aug 05 '17 at 00:59
0

If you are the only user of the code you write, then using this type of protection will only help readability and understanding for yourself when you come back in the future but it is, ultimately, not necessary for yourself other than following "best practices."

However, if others are using your code, then you may want (or need) to keep certain implementation private, not only to ensure operation as you have constructed but also to avoid accidental clobbering by those implementing your code.

rgthree
  • 7,217
  • 17
  • 21
  • If I understand what you're saying, it's basically to "protect" the code from unwanted changes? Are there any real world examples of this? –  Aug 05 '17 at 00:48
  • @00Saad Sure, look at almost any JavaScript framework. Since javascript doesn't have private/protected members on "classes" it's possible to find, use and/or overwrite properties on an object's prototype. In order to curb that, a local closure would be used to create private variables only accessible from within. Not only to prevent accidental (or purposeful) usages of properties that don't need to be exposed, but other benefits come along as well, such as smaller a smaller file size after compilation/minification, smaller memory footprint, etc. – rgthree Aug 05 '17 at 01:27