JavaScript

즉시실행함수

은찡안찡 2022. 9. 28. 12:50
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS12.즉시실행함수+객체생성자함수.html</title>
    <script>
        //익명함수는 이벤트 연결 메서드이 매개변수에서 사용한다.
        (function(){
            var num=100;
            function menu() {
                num += 100;
                alert(num);
            }

            menu();
        }());

        function CheckWeight(name, height, weight) {
            this.userName = name;
            this.userHeight = height;
            this.userWeight = weight;

            this.getInfo = function() {
                var str = '이름: ' + this.userName + ', 키: ' + this.userHeight + ', 몸무게: ' + this.userWeight + '<br>';
                
                return str;
            }

            this.getResult = function() {
                var str = '';
                var minWeight = (this.userHeight - 100)*0.9 - 5;
                var maxWeight = (this.userHeight - 100)*0.9 + 5;

                if(this.userWeight >= minWeight && this.userWeight <= maxWeight) {
                    str = "정상 몸무게입니다.<br>";
                }else if(this.userWeight < minWeight) {
                    str = "정상 몸무게보다 미달입니다.<br>";
                }else {
                    str = "정상 몸무게보다 초과입니다.<br>";
                }

                return str;
            }
        }

        var kim = new CheckWeight('김보리', 168, 62);
        var park = new CheckWeight('박달재', 175, 88);
        /*
        document.write(kim.getInfo());
        document.write(kim.getResult());
        document.write(park.getInfo());
        document.write(park.getResult());
        */
        function getInnerHtml() {
            var element = document.getElementById('info');
            element.innerHTML = kim.getInfo();
        }
    </script>
</head>
<body>
    <p id="info"></p>
    <button onclick="getInnerHtml()">버튼</button>
</body>
</html>

'JavaScript' 카테고리의 다른 글

종합문제  (0) 2022.09.28
재귀함수  (0) 2022.09.28
이미지 바꾸기  (0) 2022.09.28
리턴  (0) 2022.09.28
함수정의  (0) 2022.09.28