JavaScript

ajax, closure

Naranjito 2021. 1. 11. 13:52

Ajax : It stands for Asynchronous Javascript And XML. When click some button on the webpage, the webpage does not change. The web browser make request to the server on the background. This is asynchronous process.

 

Closure : It gives tou access to an outer function's scope from an inner function. 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <title>Document</title>
</head>
<body>
    <script>
        var numbers=[20,10,9,8,7,6,5,4,3,2,1];
        var sortfunc=function(a,b){
            return a-b;
        }
        console.log(numbers.sort(sortfunc));//sortfunc calls callback
        //sort mathod which receive callback function(sortfunc) receiving the content of sortfunc as a parameter, and then calling internally, change the sort function


        function outter(){ //outer function
            var title = 'coding everybody'; //local variable defined in the outer function
            function inner (){ //inner function
                alert(title); //if there is no title variable in inner function, it starts to search same variable in outer function
                //in other words, inner function can access local variable in outer function 
            }
            inner();
        }
        outter();
    </script>
</body>
</html>