PencilCoder

Teacher's Guide: Closures!/Function Memory!

Overview

THIS DOC IS REFERENCED BY CUSTOMFUNCTINS_NOTES.HTML W.R.T. IIFE'S

More about the lesson

Global Variables \& IIFE

"(Burnham 12) One of JavaScript's distinctive features is that all functions are closures, meaning that they have access to all variables in all surrounding scopes---regardless of where they're called from." We have seen examples of this throughout, beginning with the lesson Variable Scope.

x = 5
sumXY = -> x+y
Y=7
see sumXY()
For us, that is as much a source of complications for us to watch out for as a feature to take advantage of (described in the previous lesson as potentially reflecting poor coding style, i.e., arguments woudl be better, plusX = (y, x=5) -> return x+y.

However, the JS closure feature opens up possibilities for creating a persistent local variable scope, an in fact many sources will describe a closure that way.

excellent definition here: https://stackoverflow.com/questions/36636/what-is-a-closure

A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block. Languages which support closure (such as JavaScript, Swift, and Ruby) will allow you to keep a reference to a scope (including its parent scopes), even after the block in which those variables were declared has finished executing, provided you keep a reference to that block or function somewhere. The scope object and all its local variables are tied to the function and will persist as long as that function persists. This gives us function portability. We can expect any variables that were in scope when the function was first defined to still be in scope when we later call the function, even if we call the function in a completely different context.

"Lexical closures are most interesting in, and are usually associated with, languages that can treat functions as data (FirstClassFunctions), since storing a closure for later use implies an extension of the lifetime of the closed-over lexical environment beyond what one would normally expect. (Source: https://wiki.c2.com/?LexicalClosure)"

Notes to activities

Use a closure example of findFirstNPrimes that is more efficient b/c it remembers previously identified primes (in the current session) but without using a global variable (later on/point to IO with local storage as another alternative)

Additional activities

  • example: https://hacker.pencilcode.net/edit/33-MoreCallbacks/Save/SaveCSV

Beyond the lesson

Continuation Passing Style

https://bessiambre.medium.com/continuation-passing-style-patterns-for-javascript-5528449d3070#:~:text=Continuation%20Passing%20Style%20basically%20means,the%20last%20parameter%20of%20functions.

IIFE ---

Discussed in the technicalities to Custom Functions Notes, (and slightly mentioned in Anonymous functions notes) which references these notes...

Here is a javascripttutorial on IIFE.

do

do is a way to invoke functions, IIFE. Alternatively, use (....)()

  do (s = 25) ->
    dot random(color), s
    label "org is " + s

What can go wrong

Pedagogy

Technicalities

From this: https://stackoverflow.com/questions/14370253/can-javascripts-document-object-be-used-as-a-variable

        Actually, that is the reason why most "advanced" javascript snippets / libraries begin with a closured function scope like

  (function( window, document, undefined ) {
      // window will always reference the "window" object that got originally passed in
      // document will always reference the "document" object that got originally passed in
      // undefined will always reference the "undefined value" that got originally passed in
  }( window, window.document ));
  ..just to avoid, the such called asshole effect. If such a closure is openend at the very top of a file, it makes sure that you reference the original objects within, just in case some genius had the great idea to overwrite/overlap them.

  Conclusion: You are of course free to choose the name of your variables at will, but you really shouldn't use the name of such prominent names like window, document, undefined et cetera for obvious reasons.