Scope and closure

Scope is about the accessibility of variables. In Java, basically, a set of curly brackets {} defines a scope, including class-level scope, method-level scope, and block-level scope. 

Let's take a look at the following example in Java:

1.  public class User { 
2. private String name;
3. private List<String> interests;
4.
5. public User (String name, List<String> interests) {
6. this.name = name;
7. this.interests = interests;
8. }
9.
10. // Check if a user is interested in something
11. public boolean isInterestedIn(String something) {
12. boolean interested = false;
13. for (int i = 0; i < interests.size(); i++) {
14. if (interests.get(i).equals(something)) {
15. interested = true;
16. break;
17. }
18. }
19. return interested;
20. }
21. }

The name and interests properties are in the class-level scope and they are accessible anywhere within the class. The interested variable, defined in line 12, is in method-level scope, and it is only accessible within that method. The i variable, in line 13, is defined within the for loop and it is block-level scope only accessible within the for loop block. In Java, the scope of the variables is static and can be determined by the compiler.

In JavaScript, the scope of the variables is much more flexible. There is global scope and function scope, and block scope with the let and const keywords, which were introduced in ES6, which we will talk about later.

Let's look at the following JavaScript example:

1.  function bookHotel (city) {
2. var availableHotel = 'None';
3. for (var i=0; i<hotels.length; i++) {
4. var hotel = hotels[i];
5. if (hotel.city === city && hotel.hasRoom) {
6. availableHotel = hotel.name;
7. break;
8. }
9. }
10. // i and hotel still accessible here
11. console.log('Checked ' + (i+1) + ' hotels');// Checked 2 hotels
12. console.log('Last checked ' + hotel.name); // Last checked Hotel B
13. {
14. function placeOrder() {
15. var totalAmount = 200;
16. console.log('Order placed to ' + availableHotel);
17. }
18. }
19. placeOrder();
20. // Not accessible
21. // console.log(totalAmount);
22. return availableHotel;
23. }
24. var hotels = [{name: 'Hotel A', hasRoom: false, city: 'Sanya'},
{name: 'Hotel B', hasRoom: true, city: 'Sanya'}];
25. console.log(bookHotel('Sanya')); // Hotel B
26. // Not accessible
27. // console.log(availableHotel);

The hotels variable declared in line 24, is in global scope and is accessible anywhere, such as inside the bookHotel() function, even though the variable is defined after the function. 

The availableHotel variable declared in line 2 is in the scope of the bookHotel() function. It is a local variable and is not accessible outside of the function, as you can see from line 27. Inside its enclosing function, the availableHotel variable is accessible anywhere, even the nested placeOrder() function, as you can see in line 16. This is called closure. A closure is formed when a function is nested inside another function. And no matter how deeply you have nested a function, it will still have access to its parent function's scope, and all the way to the top scope, which is global scope. The totalAmount variable, defined in line 15, is a local variable of the placeOrder() function.

And in lines 3 and 4, we defined the i and hotel variables with the var keyword. Even though it is in a for loop block, it is still accessible outside the block, as shown in lines 11 and 12. In ES6, you can use the let keyword to define i and hotel, which will put these two variables in for loop block scope. We will talk more about this later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.34.146