the interviewer asks, what is a closure and how should I answer it?
I want to answer a paragraph or definition, not a function instance
the interviewer asks, what is a closure and how should I answer it?
I want to answer a paragraph or definition, not a function instance
recommended elevation, online understanding is personal, you can refer to, but have their own understanding is the best.
I think the better answer is:
1. Closure conditions: a, outer function nesting inner function; b, inner function referencing variables in outer function; c, outer function returning inner function;
2, js garbage collection mechanism: when a variable is not referenced, it is recycled
3. When each function is declared, an active object containing variables within the scope of the function is generated
give a simple example to analyze:
function a() {
var x = 1
var y = 100
console.log(y)
yPP
return function b() {
console.log(xPP)
}
}
var result= a() // 100
var result1 = a() // 100
-----------------------------
result() // 1
result() // 2
in the above example, when there is no closure, I destroy the function scope after I execute a, then x is also destroyed, so the result of two execution should be twice 1. But the second time is 2, which means that the internal x has not been destroyed, that is, the x in the scope of a function has been saved.
starts the analysis with the above three points: when the
a function is declared, an active object containing the variables within the scope of the a function is created, which can be called the local parameter x inside the aa,aa; when the
b function is declared, the x in aa is referenced; after the
a function is executed, the global variable result refers to the b function.
the garbage collection mechanism combined with JS, x references will not be destroyed, b references will not be destroyed, result is a global variable will not be destroyed, x can be retained, thus forming a closure. Because the y in aa is not referenced, it is destroyed.
above is my understanding. Finally, I can answer the application of closures. I won't answer them here. There are many
on the Internet.to sum up, the suggested answer is to add the above three points to the application
closures are functions that have access to variables in the scope of another function. The "JavaScript Advanced programming"
closure is a function nested function, and variables outside the function can be used inside the function.
A function that can access variables of an external function, for example:
function a(x){
var z=3;
function b(y){ // axz
console.log(x+y+(PPz));
}
return b;
}
the first-level function calls the second-level function, and the second-level function accesses the first-level function lexical scope variable is called closure, and the simpler point is the function nest function
remember which book you read, and the best way to understand closures is to compare it with the concept of class :
explanation: the closure is formed when the current function can access variables in other scopes.
function:
1. Expand the scope of variables
2. Prevent variables from being destroyed (garbage collection mechanism)
define function a , function b
satisfy:
forms a closure .
how to answer each person's expression, just mean it.
//
function b(){
var z = 3;
function a(y){
console.log(z + y); // a b z
}
return a;
}
var c = b();
c(1); // a b
popularly speaking: there is a function in the function; further understanding: the wrapped function can access the variables of the wrapped function;
after looking at all the answers, everyone's understanding is the same, except for one thing: does the closure have to be accessed by the outside world?
the answer is: No!
function a(x){
var z=3;
function b(y){ // axz
console.log(x+y+(PPz));
}
return b;
}
is it still a closure to remove return?
the answer is: Yes!
it is both an internal function and a closure, and the two do not conflict.
definition of closures on mdn:
Aclosure is a combination of a function and the lexical environment in which it is declared.
as long as it is satisfied, this is a function, which uses external variables inside, and it is a closure. The first example on
mdn:
function init() {
var name = "Mozilla"; // name init
function displayName() { // displayName() ,
alert(name); //
}
displayName();
}
init();
because the diaplayName cannot be accessed again, the name in the closure is also recycled.
by definition, displayName is a closure, although it has no use for eggs.
-split line--
in addition, by definition, any function can be a closure in a broad sense, because external references in the lexical environment may be empty.
so if the interviewer asks you about closures, I suggest you ask him first whether to answer the definition of closures or most practices, in a narrow sense or in a broad sense.
I think this is not bad. I don't quite understand closures either. https://codeshelper.com/a/11...
function nesting function
Previous: Laravel ajax
The horizontal coordinates of echarts are not specifically marked, and the X coordinates do not have a grid to indicate. Here is a figure that specifies . js * * createChartSix() { this.$http .get(this.$api.dataChart) .then(rsp => { ...
sincerely ask for advice: < hr > I want to use node as the background to build a video streaming server. The front end is similar to Youku VOD. It can record the playback node function, and load the progress bar at any point (starting from the c...
I need to implement in a chained promise function, any function error in the middle terminates the program, and can catch the error function, and then perform different operations according to different error functions. How can the following code be imp...
I would like to ask what is wrong with this code. Thank you for your answers. ...
is doing a question on Niuke.com. I encounter a problem: to achieve the function callIt, after the call meets the following conditions 1, the returned result is the result after calling fn 2, and the call parameter of fn is all the parameters after th...
A timing examination system is triggered if the click event is triggered within 5 seconds, and if it is not triggered, the system marks the correct answer at the end of the countdown. How to implement ...
description: a regular match is given to the content of an input box, and the matching content is the product activation code. looks like this: "0C31-0B81-BB32-3094-0C31-0B81-BB32-3094 " Code: $( -sharplicenseCode ).keyup(function () { le...
1. The custom event triggers the click event of .cpcstartrefresh, but triggers 2 click events each time 2. The code is as follows: window.onmessage = function (e) { create an event object, var myEvent = document.createEvent( Event ); m...
I configured the MIME type of the file with the amr suffix in the apache configuration as application ms-download, Why it is a garbled page when opening a file in amr format using window.open in chrome, while a file in amr format can be successfully dow...
I got a set of data, which is to choose the type of question. How can I tell if I have chosen the right one answera: "Olympic Games " answerb: "Asian Games " answerc: "Paralympic Games " answerd: "University Games " id: "1772 " question: "f...
read a number, such as 521 change this number to 0521 but put it into four div separately, how to realize it? 0 < div > 5 < div > 2 < div > 1 < div > ...
react projects cannot save store? using redux, data index.js ReactDOM.render( <Provider store={store}> <Router > < Provider>, document.getElementById( root )) registerServiceWorker() actions export const SET_USER = ...
how to find all the NA characters in a HMLT page and replace it with "and invalid " with native JS ...
...
suppose there are only a.js and b.js (only two js and nothing else) b.js export A variable is used for a.js can you directly use import and export without using packaging and compilation tools such as webpack or babel can it be achieved with the h...
do the gods have plug-ins for uploading attachments in the mobile version that can also be uploaded more than one? ...
what does the code circled in the following picture mean? ...
I look at a html ui project with items such as: var apiserver = window[ apiserver ]; url = apiserver + "filterUrlController getUrlData"; I searched the whole project and couldn t find the value of window [ apiserver ]. How do you un...
In the vue-cli project, I directly introduced the particls library into index.html, and the related files are placed in the static directory . <div id="particles-js">< div> <script type="text javascript" src="s...
How does the code return only the result after the last asynchronous operation in the for loop is completed as shown in the figure? Please stop by and give me a hand. ...