@@ -173,46 +173,42 @@ sensibly enough, an anonymous function.
173173As a thought experiment, what happens here:
174174
175175``` js
176- (function (x ){ return x + 3 })(2 ) // => ???
176+ (function (baseNumber ){ return baseNumber + 3 })(2 ) // => ???
177177```
178178
179- We recognize the first ` () ` as being those that we might use from arithmetic:
179+ We recognize the first ` () ` as being those that we might use from arithmetic to
180+ make something happen first in terms of order-of-operations. In the example
181+ below, ` * ` should happen first, but instead it happens after the subtraction
182+ since the ` () ` evaluation is performed earlier:
180183
181184``` js
182185( 3 - 4 ) * 2 // => -2
183186```
184187
185- So the first parentheses return the anonymous function, the potential to do
186- work. The second ` () ` are the ` () ` of function invocation. So we're "invoking"
187- the function immediately after defining it.
188+ So in the IIFE statement, the first parentheses return an anonymous function.
189+ It's like we assigned an anonymous function to a variable name, but didn't do
190+ the actual assignment and are instead left the right hand side of the
191+ assignment: a thing that can be invoked (instantly).
192+
193+ The second ` () ` are the ` () ` of function invocation like ` sayHello() ` having
194+ ` () ` after the ` g ` . It means, do the work in the function identified by the
195+ name ` sayHello ` .
196+
197+ Put these two components together and we're "invoking" the function
198+ immediately after defining it. That would make the noun we're doing that upon
199+ an "Instantly-Invoked Function Expression (IIFE for short)."
188200
189201``` js
190- (function (x ){ return x + 3 })(2 ) // => 5
202+ (function (baseNumber ){ return baseNumber + 3 })(2 ) // => 5
191203```
192204
193- This bit of code is called an IIFE or "Instantly Invoked Function Expression."
194- This stands to reason since it's a function expression that we run immediately.
195- IIFEs are a good way to make sure that you're grasping the content of this
196- lesson up to this point.
197-
198205Interestingly, any variables, functions, ` Array ` s, etc. that are defined
199206_ inside_ of the function expression's body _ can't_ be seen _ outside_ of the
200207IIFE. It's like opening up a micro-dimension, a bubble-universe, doing all the
201208work you could ever want to do there, and then closing the space-time rift.
202209IIFEs are definitely science fiction or comic book stuff recalling the plot
203210of "Donnie Darko" or Dr. Strange's "mirror dimension."
204211
205-
206- ``` js
207- (
208- function (thingToAdd ) {
209- let base = 3
210- return base + thingToAdd
211- }
212- )(2 ) // => 5
213- // console.log(base) //=> Uncaught ReferenceError: base is not defined
214- ```
215-
216212We'll see some of the practical power of "hiding things" in IIFEs a little
217213later in this lesson.
218214
0 commit comments