The most puzzling interview question at the front end: could you tell me an example of how you used closures in your work? Ask for an example

an example of the use of closures at work, which was asked by the interviewer. To be honest, I have hardly used it at work, but I know the principle of closures. Speaking of usage scenarios, I really don"t know. Ask the great god for some examples.

Mar.20,2021

closures are used either to access variables inside the function, to save a variable in memory, or to protect internal variables of the function.
1. In order not to confuse global variables and pollute the global scope, we can use closures instead of global variables (this is the case with JQuery internal encapsulation).
2, and also commonly used to create similar block-level scopes (that is, when binding events for each element in a list of nodelist objects);
3, the use of expensive functions is also reflected.
. Usually we use closures a lot, but before you know it, you have already created closures


first of all, maybe the subject looks at closures too narrowly, and some common patterns are actually closures. To be fair, however, the typical closure usage (save state) is now obsolete.

what is

closures are defined as MDN , in other words, functions with context (context, state, attributes, local variables, find a word you can understand). Since ES2015, the simplest closure becomes like this:

  

the earliest jq is written in closures

Menu