Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a right statement for Closure?

Quote from Stoyan Stefanov's Object-Oriented JavaScript (page 84):

enter image description here

If you're at point a, you're inside the global space. If you're at point b, which is inside the space of the function F, then you have access to the global space and to the F-space. If you're at point c, which is inside the function N, then you can access the global space, the F-space and the N-space You cannot reach from a to b, because b is invisible outside F. But you can get from c to b if you want, or from N to b. The interesting thing—the closure—happens when somehow N breaks out of F and ends up in the global space."

I think the bold sentence above should be changed to "If you're at point c, which is inside the function N, then you can access the global space and the N-space " (the F-space shouldn't be contained, because the point c only has access to N-space and the global scope G. ).

Am I right? thanks.

like image 479
Matt Elson Avatar asked Oct 05 '12 10:10

Matt Elson


People also ask

How do you use the word closure?

(1) The closure of the mine led to large-scale redundancies. (2) The accident caused the complete closure of the road. (3) Several military bases are threatened with closure . (4) We haven't yet been told officially about the closure.

Where can I use closure?

Closures are frequently used in JavaScript for object data privacy, in event handlers and callback functions, and in partial applications, currying, and other functional programming patterns.

What is a good closing sentence for a letter?

If you want to be very formal in closing your business letter, consider using one of these phrases: Respectfully. Yours sincerely. Yours respectfully.


1 Answers

No, I think what it's saying is that N is a function that has been returned from function F, and therefore has access (through a closure) to variable b which was declared inside F. For example (live example):

function F() {
    var b = 10;
    return function () {
        console.log(b);
    };      
}

var N = F(); //N is a reference to the anonymous function returned from F

N(); //logs '10' because we still have access to b (because of the closure)
like image 74
James Allardice Avatar answered Oct 05 '22 09:10

James Allardice