anonymous functions (*)

devfish·2023년 1월 4일
0

Javascript

목록 보기
12/30

Immediately invoked anonymous function

  • anonymous, as the name suggests, allows creating a function without any names identifier. It can be used as an argument to other functions

  • functions that wrap our code and create an enclosed scope around it, which help keep any state or privacy within that function

    (function() {
     'use strict';
     // Your code here
     // All function and variables are scoped to this function
    })();
     
     (function() {
       alert("Hello");
    })()
    
  • it can also be used as an argument for other functions

setTimeout(function() {
   alert('Demo');
}, 3000);

<!DOCTYPE html>
<html>
   <body>
      <h2>JavaScript Closures</h2>
      <script>
         var p = 20;
         function a() {
            var p = 40;
            b(function() {
               alert(p);
            });
         }
         function b(f) {
            var p = 60;
            f();
         }
         a();
      </script>
   </body>
</html>
profile
la, di, lah

0개의 댓글