'자바스크립트'에 해당되는 글 4건

  1. 2012.08.29 자바스크립트 계산기
  2. 2012.08.27 가변인자
  3. 2012.08.27 함수 호출
  4. 2012.08.27 함수의 생성과 출력

 

 

실행주소 : http://bboong100.cafe24.com/0828/calc1.html

풀 소스

<html>
<head>
    <script type="text/javascript">
        function cal(num) {
            calc.txt.value += num;
        }
        function answer() {
            calc.txt.value = eval(document.calc.txt.value);
            // document.calc.txt.value 에 저장된 문자열을
            // 자바스크립트코드로 실행
        }

        function clearcal() {
            calc.txt.value = "";
            // 텍스트창 초기화
        }

        function bs(){
            calc.txt.value = calc.txt.value.substring(0, calc.txt.value.length-1);
            // substring 으로 0부터 value 길이에서 -1 한 만큼의 길이를 출력한다
        }
    </script>
    <title>계산기</title>
</head>
<body>
<form name="calc">
    <table border=1 width=200px height=200px cellpadding=0 cellspacing=0>

        <tr border=2>  <!-- 숫자표시창 -->
            <td colspan=5 align=center><input type=text name="txt" disabled></td>
        </tr>
        <tr height=40px> <!-- 백스페이스, C -->
            <td colspan=5>
                <table border=1 width=100%>                                               
                    <tr align=center>
                        <td width=33%><input type=button value='BackSpace' style="width:100%;" onclick=bs()></td>
                        <td width=33%><input type=button value='C' style="width:100%;" onclick=clearcal()></td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr align=center>
            <td width=25%><input type=button value=1 onclick="cal('1')"></td>
            <td width=25%><input type=button value=2 onclick="cal('2')"></td>
            <td width=25%><input type=button value=3 onclick="cal('3')"></td>
            <td width=25%><input type=button value='/' onclick="cal('/')"></td>
        </tr>
        <tr align=center>
            <td><input type=button value=4 onclick="cal('4')"></td>
            <td><input type=button value=5 onclick="cal('5')"></td>
            <td><input type=button value=6 onclick="cal('6')"></td>
            <td><input type=button value=* onclick="cal('*')"></td>
        </tr>
        <tr align=center>
            <td><input type=button value=7 onclick="cal('7')"></td>
            <td><input type=button value=8 onclick="cal('8')"></td>
            <td><input type=button value=9 onclick="cal('9')"></td>
            <td><input type=button value=+ onclick="cal('+')"></td>
        </tr>
        <tr align=center>
            <td> </td>
            <td><input type=button value=0 onclick="cal('0')"></td>
            <td><input type=button value='=' onclick="answer()"></td>
            <td><input type=button value='-' onclick="cal('-')"></td>
        </tr>
    </table>
</form>
</body>
</html>

 

Posted by 공돌공돌
,
<script type="text/javascript">
        function sumAll() {
            alert(arguments + ': ' + arguments.length);
        }
        sumAll(1,2,3,4,5,6,7,8,9);
    </script>

가변인자 함수는 매개 변수의 개수가 변할 수 있는 함수이다

매개 변수를 선언된 형태와 다르게 사용했을 때도 매개 변수를 모두 활용할 수 있다

위 소스는 argument(매개 변수의 배열)의 배열의 길이를 출력하는 예제이다

함수를 호출할 때 9개를 입력 했으므로 길이는 9로 출력된다

 

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

내부함수  (0) 2012.08.27
리턴값  (0) 2012.08.27
매개변수  (0) 2012.08.27
함수 재정의  (0) 2012.08.27
함수 호출  (0) 2012.08.27
Posted by 공돌공돌
,

    <script type="text/javascript">
        var a =
                function () {
                    var output = prompt('숫자를 입력해주세요.', '숫자');
                    alert(output);
                };
       a();
        //a를 호출하여 결과를 나타낸다

    </script>

 

 

 

 

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

리턴값  (0) 2012.08.27
가변인자  (0) 2012.08.27
매개변수  (0) 2012.08.27
함수 재정의  (0) 2012.08.27
함수의 생성과 출력  (0) 2012.08.27
Posted by 공돌공돌
,

    <script type="text/javascript">
        //        함수의 생성과 출력
        var a =
                function () {
                    var output = prompt('숫자를 입력해주세요.', '숫자');
                    alert(output);
                };
        alert(a);
        // a 의 소스내용을 출력해준다
    </script>

 

 

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

리턴값  (0) 2012.08.27
가변인자  (0) 2012.08.27
매개변수  (0) 2012.08.27
함수 재정의  (0) 2012.08.27
함수 호출  (0) 2012.08.27
Posted by 공돌공돌
,