함수 선언과 실행

소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    
    <script>
        var a = func1();    //먼저 func1()실행
        document.write("  func1() : "+a+"<br/>");   //리턴받은값 1000을 a에서 출력
        document.write("<hr/>");    
        func2();            //func2(a,b)가 실행
        document.write("<hr/>");    
        func2(1,2);         //func2(a,b)가 실행
        document.write("<hr/>");
 
        var func2 = function(){         //익명클래스와 같은 느낌 익명함수. 
            document.write("실행<br/>")  //자료형의 마지막은 함수형
        }
        func2();
 
        function func1(){
            document.write("javasc..");
            return 1000;
        }
        function func2(){
            document.write("func2e");
            return;
        }
         function func2(a,b){
            document.write("func2"+"<br/>");
            document.write("a="+a+", b="+b);
            return;
        }    
        
    </script>
</head>
<body>
    
</body>
</html>
cs

결과


+추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    
    <script>
        var a = func1();    //먼저 func1()실행
        document.write("  func1() : "+a+"<br/>");   //리턴받은값 1000을 a에서 출력
        document.write("<hr/>");    
        func2();            //func2(a,b)가 실행
        document.write("<hr/>");    
        func2(1,2);         //func2(a,b)가 실행
        document.write("<hr/>");
 
        var func2 = function(){         //익명클래스와 같은 느낌 익명함수. 
            document.write("실행<br/>")  //자료형의 마지막은 함수형
        }
        func2();
 
        function func1(){
            document.write("javasc..");
            return 1000;
        }
        function func2(){
            document.write("func2e");
            return;
        }
         function func2(a,b){
            document.write("func2"+"<br/>");
            document.write("a="+a+", b="+b);
            return;
        } 
        
        document.write("<hr/>"); 
        document.write("<hr/>"); 
        var arr1=[1,100,func2,300];
        arr1[2](); //하면 배열에 들어간 함수가 실행됨. 
 
        var obj1={'a':"aa"'b':func2, 'c':1000};
        obj1.b();
    </script>
    
</head>
<body>
    
</body>
</html>
cs

추가 된 부분

        document.write("<hr/>"); 
        document.write("<hr/>"); 
        var arr1=[1,100,func2,300];
        arr1[2](); //하면 배열에 들어간 함수가 실행됨. 
 
        var obj1={'a':"aa"'b':func2, 'c':1000};
        obj1.b();

 결과


콜백함수

소스1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        function func1(){
            document.write("func1()<br/>");
        }
        function func3(){
            document.write("func3()<br/>");
        }
        function func2(a){
            a();
            return;
        }
        
        func1();
        func2(func3); //callback
        func2(function(){
            document.write("<h1>aaa</h1>");
            return;
        });
    </script>
</head>
<body>
    
</body>
</html>
cs

결과1


소스2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
       var func1 = function(){
            alert("func 1");
            return;
        };/*선언*/
        func1();/*실행*/
        
        (function(){
            alert("func 0");
            return;
        })();/*선언과 동시에 실행*/
        
        var $ = function(a){
            a();
            return;
        };
        $(function(){
            var a = 100;
            alert('a : '+a);
        });
    </script>
</head>
<body>
    
</body>
</html>
cs

결과2 (순서대로 확인버튼 누른것이다)


'* Programming > JavaScript' 카테고리의 다른 글

THIS  (0) 2016.10.05
클로져(Closure)  (0) 2016.10.05
Math.floor() / Math.round()  (0) 2016.10.04
*JSON  (0) 2016.10.04
구구단 만들기  (0) 2016.10.04

+ Recent posts