Skip to content
Advertisements

Closures and Recall Functions

They are included in the functions defined in the functions that are sent as parameters for a function or function in a function defined in JavaScript functions. Therefore, they can use the variables (variable scope) and values ​​within the scope of the function they are defined in.

This is a very useful feature commonly used in JavaScript programming. If JQuery, Node.js etc. If you are using a library or technology, you should understand this very well.

Sample:

function mes() {
            var text = "hello";
            function mesShow() {
                document.write(text)
            }
            return mesShow; 
        }
        var fonksiyon = mes();
        fonksiyon();

output : hello

In the example, the message called the function called the messageGoster function is a function defined in the message function. We assign this function to a variable named function and then run it. The function () calls the messageGoster function, which is the main function, where the function takes its value (as previously mentioned, the functions are values).

Here’s the point. Normally, the text variable is not defined at the location of the function (). However, because the text variable and the actual function are defined in the same place, the function can recognize the value of the text variable when it is executed from outside.

We can write the same example:

function mes() {
            var text = "hello";
            return function mesShow() {
                document.write(text)
            }
        }
        mes()()

Imagine that there is a function where the message () returns a function. If the second () function is executed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: