ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바스크립트 3일차_강사소스
    교육/자바스크립트 2014. 9. 4. 10:00

    [day3.html]

     

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='utf-8'>
        <title>자바스크립트 3일</title>
    </head>
    <body>
        <script>
        function arrayWalk(data, f){
            for(var i in data){
                f(i, data[i]);
            }
        }

        function showElement(key, value){
            console.log(key + ':' + value);
        }

        var arr = [1,2,4,8,16];
        arrayWalk(arr, showElement);
        </script>


        <script>
        function closure(init){
            var count = init;

            return function(){
                return ++count
            };
        }

        var myClosure = closure(1);
        console.log(myClosure());
        console.log(myClosure());
        </script>
    </body>
    </html>

     

    [htmlutil.js]

     

    function genTableRow(data){
        // 함수를 호출할 때마다 테이블의 행을 하나 새로 생성
        var c_entry_index=0;

        return function(){
            var entry = data[c_entry_index++];
            var tableRow = '';
            var item_index;
            for(item_index in entry){
                tableRow += wrapWithTag(entry[item_index],'td');
            }

            return tableRow;
        }
    }

    // HTML 테이블 생성 함수 만들기
    function arrayToHtmlTable(data){

        // HTML 테이블 생성하기
        var html_table = '<table>';

        // 항목 행 생성 클로져
        var genTr = genTableRow(data);

        var i;
        for(i in data){
            // 각 행을 생성해 추가한다.
            html_table += wrapWithTag(genTr(), 'tr');
        }

        html_table += '</table>';

        return html_table;

    }

    function wrapWithTag(src, tag){
        var result = '<' + tag + '>';
        result += src;
        result += '</' + tag + '>';

        return result;
    }

     

    [coffee.js]

    var Coffee = function(data){
       
        this.imageFilename = data[2];

        var now = new Date();
        var month = now.getMonth()+1;

        // 여름인지 판정하기
        var isSummer = month > 5 && month < 9;

        this.productName = (isSummer)? '아이스 ' + data[0]:data[0];
        this.price = (isSummer)? data[1]+0.5:data[1];
    };

    // 테이블로 만들 때 사용할 배열을 반환하는 메소
    Coffee.prototype.getAsArray = function(){
        // 이미지 태그 만들기
        var img = '<img ';
        img += 'src="' + this.imageFilename + '" ';
        img += 'width="50"';
        img += '>'

        // 결과로 내보낼 배열 생성
        var data_array = [img, this.productName, this.price];
        data_array.push('<input type="text">');

        return data_array;
    };

     

    coffee.js

     

    coffee_auto_table_with_func.html

     

    day3.html

     

     

    htmlutil.js

     

     

     

     

    '교육 > 자바스크립트' 카테고리의 다른 글

    자바스크립트 4일차_내 소스  (0) 2014.09.05
    자바스크립트 3일차_내 소스  (0) 2014.09.04
    자바스크립트 2일차  (0) 2014.09.03
    자바스크립트 1일차  (0) 2014.09.03
Designed by Tistory.