PlayData/WebUI

[Day20 | JavaScript] Function, Object

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 12. 8. 16:15

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํ—ˆ์ ‘ํ•œ ์–ธ์–ด์—์„œ ์‹œ์ž‘๋˜์—ˆ๋‹ค! 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ต๋ช…ํ•จ์ˆ˜

 

 

Type01.html

๊ฐ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ์•Œ์•„๋ณด์ž 

<html>
    <body>
        <script>

            var a = 10; 
            var b = true; 
            var c = "Korea";
            var d = function() { } ;
            var e = { } ;
            var z; // undefined ==> null 
    

            alert(a + "," + typeof(a)); //number
            alert(b + "," + typeof(b)); //boolean
            alert(c + "," + typeof(c)); //string
            alert(d + "," + typeof(d)); //function
            alert(e + "," + typeof(e)); //object
            alert(z + "," + typeof(z));

        </script>
    </body>
</html>

 

 

 

Op02.html

ํŒŒ์ผ์ด html์ด๋ฉด ํŒŒ์ผ ๋‚ด ๊ตณ์ด ์•ˆ์จ์ฃผ๊ณ  ์Šคํฌ๋ฆฝํŠธ๋งŒ ์จ์ค˜๋„ ์‹คํ–‰ ๊ฐ€๋Šฅ

์™œ ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ i++์ด 10์ด ๋‚˜์˜ค๋Š”์ง€ ์ดํ•ดํ•˜๋ฉด ๋จ 

<script>
    var i = 10; 
    alert(i++); // i++ ์ด 10์ด ๋‚˜์˜ค๋Š” ์ด์œ ๋ฅผ ์ดํ•ดํ•˜๋ฉด ๋จ 

    var s = '';
    s += 'Korea';
    s += '!!';
    s += 'ํ™”์ดํŒ…';

    alert(s);

</script>

 

 

 

 

prompt01.html

<script>
    var a = prompt('์ž…๋ ฅ์‚ฌํ•ญ', '์•„๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”'); //ํ™๊ธธ๋™'; 
    alert(a + '๋‹˜, ์–ด์„œ์˜ค์„ธ์š”!')

    var b = confirm('ํ™•์ธํ•˜์…จ์–ด์š”?');
    alert(b);
</script>

 

 

Quiz

์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ

90์  ์ด์ƒ์ด๋ฉด ์šฐ์ˆ˜

80์  ์ด์ƒ์ด๋ฉด ๋ณดํ†ต

70์  ์ด์ƒ์ด๋ฉด ๋ถ€์กฑ ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค

(promt, alert ์‚ฌ์šฉ) 

<html>
    <body>
        <script>
            // ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ 90์  ์ด์ƒ์ด๋ฉด ์šฐ์ˆ˜ / 80๋ฒ” ์ด์ƒ์ด๋ฉด ๋ณดํ†  / 70์  ์ด์ƒ์ด๋ฉด ๋ถ€์กฑ 
            var a = prompt('์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”', '์ ์ˆ˜ ์ž…๋ ฅ๋ž€'); 
            
            if (a >= 90) {
                alert("์šฐ์ˆ˜");
            } else if (a >= 80) {
                alert("๋ณดํ†ต");
            } else if (a >= 70) {
                alert("๋ถ€์กฑ");
            } else {alert("๋‚™์ œ")} ;      
            
        </script>
    </body>
</html>

 

Arr03.html

๋‹ค ๋‹ค๋ฅธ ํƒ€์ž…๋„ ํ•œ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. 

<script>
    var a = ['Hello', 10, true, function() {}, {}, 'Hi'];

    for (var i in a ){
        alert(a[i]);
    }
</script>

 

 

 

 

Function

Func01.html

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ด 

a๋Š” function ๋ถ€ํ„ฐ ์ญ‰ ๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Œ 

 

์ต๋ช…ํ•จ์ˆ˜ 

์•ฝ์ : ๋ณ€์ˆ˜๋กœ ์ง€์ •๋˜๋ฏ€๋กœ, ๋‚˜์ค‘์— ์žฌํ• ๋‹น๋  ์ˆ˜ ์žˆ์Œ 

<script>
    var a = function(){
        alert('Func01');
        alert('์ต๋ช…ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.');
    }
    a();
    alert(a+' -> ' + typeof(a)); 
</script>

 

 

์„ ์–ธ์  ํ•จ์ˆ˜ 

<script>
    function b() {
        alert('b ํ•จ์ˆ˜๋Š”');
        alert('์„ ์–ธ์  ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.')
    }
    b();
    alert(b + '-> ' + typeof(b));
</script>

a์™€ b์˜ ์ฐจ์ด๋Š” ์ด๋ฆ„์ด ์žˆ๋Š๋ƒ, ์—†๋Š๋ƒ์˜ ์ฐจ์ด 

 

 

๊ต์ฐจ๋œ์ง€ ๋ชจ๋ฅด๊ณ , 

์ต๋ช…ํ•จ์ˆ˜์—์„œ ์ค‘๋ณต๋˜์„œ ๋ณ€์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉด 

๋ฎ์–ด์”Œ์—ฌ์ง„๋‹ค. 

<script>
    var a = function() {
        alert('AAAA');
    }
    a();
    alert('--์ •์ƒ์‹คํ–‰');
    var a = function(){
        alert("BBBB");
    }

    a();
</script>

 

์„ ์–ธ์  ํ•จ์ˆ˜๋Š” ์ตœ์ข…์ ์ธ ๊ฒƒ๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ„๋‹ค

์ต๋ช…ํ•จ์ˆ˜๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

<script>
    function c() {
         alert('CCCC');
    }

    function c() {
         alert('DDDD'); // ์„ ์–ธ์  ํ•จ์ˆ˜๋Š” ์ตœ์ข…์ ์ธ ๊ฒƒ๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ„๋‹ค
    }
    c();
    var a = function() {
        alert('AAAA'); // ์ต๋ช…ํ•จ์ˆ˜๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 
    }
    a();
    alert('--์ •์ƒ์‹คํ–‰');
    var a = function(){
        alert("BBBB");
    }

    a();
</script>

 

 

 

 

์ด๋„ˆํ•จ์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , 

๋ฐ–์— ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค๊ณ  ํ•ด๋„

์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค. 

Func05.html

<script>
    function func(i, j){
        function pow(k){return(k * k);}
        function sum(a, b){return(a+b);}

        return sum(pow(i), pow(j));
    }

    function pow(k){
        return (k + 1000);
    }

    alert(func(3,4));
    alert(func(2,7));

</script>

 

 

 

 

Func06.html

callBack ํ•จ์ˆ˜ 

๊ณ„์† ๋Œ๊ณ ์žˆ๋Š” ํ•จ์ˆ˜์— ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์ž๊ฐ€ ์žˆ๋Š” ๊ฒƒ. 

ex) ์ž๋™ ์„ธ์ฐจ์žฅ์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋จ. A์ฐจ, B์ฐจ, C์ฐจ 

<script>
    
    function callBack01(z){
        for( var i = 0; i<3 ; i++){
            z();
        }
    }


    var a = function() { alert(" AAAA ");}
    var b = function() { alert(" BBBB ");}
    var c = function() { alert(" CCCC ");}

    callBack01(a);
    callBack01(b);
    callBack01(c);
    
//////////////////////////////////////////////

    function callBack02(z, n){
        for( var i = 0; i<n ; i++){
            z();
        }
    }


    callBack02(a, 2);
    callBack02(b, 4);
    callBack02(c, 5);

</script>

 

 

 

Object.html

<script>
    // Array ์ƒ์„ฑ
    var a = ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '๊ทค', 'ํฌ๋„'];
    alert(a);
    alert(typeof(a)); // Object - Arraylist
    alert(a[1]);// ๋ฐ”๋‚˜๋‚˜๋ฅผ ์ถœ๋ ฅํ•˜๋ ค๊ณ  ํ•œ๋‹ค


    // ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
    var book = {title: 'Java',
                author: 'Tom',
                price: 40000,
                bPrint: function() { 
                    alert(this.title + ', ' + this.author + ', ' + this.price); 
                }              
    }

               
    alert(book);
    alert(typeof(book)); // Object - Arraylist
    alert(book['author']);
    book.bPrint();
    book['bPrint']();//๊ถŒ์žฅํ•˜์ง€ ์•Š์Œ
</script>

object02.html

<script>
    var book = { } // ๋นˆ book object 
    
    book.title = 'HTML'; // book object ์˜ ์†์„ฑ ์ถ”๊ฐ€ 
    book.['author'] = 'Jane';
    book.getTitle = function(){ return this.title;} // book object ์˜ ๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€ 
    book['getAuthor'] = function() {return this.author} 
    book.pBook = function() {alert(this.author); }

    alert(typeof(book));
    alert(book['title']);
    alert(book.author);

    alert(book.getTitle());
    alert(book.getAuthor());

    book.pBook();

</script>

 

 

Object03.html

1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์˜ค๋ธŒ์ ํŠธ๋Š” ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, ๋˜๋Š” ์ž๋ฐ”์˜ ํด๋ž˜์Šค์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ. 

2. ์†์„ฑ ์ •๋ณด์— ํ•œ๊ธ€์„ ์จ๋„ ๊ดœ์ฐฎ๋‹ค!

3. ํ•จ์ˆ˜ ์“ธ ๋•Œ this.๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ด๋ ‡๊ฒŒ๋„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค! 

<script>
    var dict = {
        '์‚ฌ๊ณผ' : 'apple',
        '๋ฐ”๋‚˜๋‚˜' : 'banana',
        'ํฌ๋„': 'grape',
        '๊ฐ•': 'river',
        '๋ฌผ': 'water'
    }
    
    alert(typeof(dict));
   
    dict.toString = function() {
        var output = '';

        for (var k in this) {
            if (k != 'toString'){
                output += k + ' : ' + this[k] +'\n';
            }
            
        }

        return output;
        //return (this.์‚ฌ๊ณผ + this.๋ฐ”๋‚˜๋‚˜ + this.ํฌ๋„ + this.๊ฐ• + this.๋ฌผ);
    }

    // dict.toString = function() { 
    //                 return (this.์‚ฌ๊ณผ + this.๋ฐ”๋‚˜๋‚˜ + this.ํฌ๋„+ this.๊ฐ•+ this.๋ฌผ);
    //             }   
    
    // delete(dict.๊ฐ•);
    alert(dict.toString());
</script>

 

 

์–˜๋„ ์‰ฝ์ง€์•Š๋„ค

 

 

 

 

์•…!