프로그래밍/Js

[ES6+] 버튼 클릭시 클랙스 메소드 바로실행(if문 사용 안함)

소행성왕자 2021. 11. 4. 11:25

 

<!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>Document</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://t-4user.yeskey.or.kr/v1/fincert.js?dt=20200814"></script>
</head>
<body>

    <button id="issue" class="btn">인증서발급/재발급</button>    
    <button id="renew" class="btn">인증서 갱신발급</button>    
    <button id="renew2" class="btn">인ㅅㄴㅅ</button>    
    
<script>
    
    FinCert.Sdk.init({
        "orgCode": "D200800001",
        "apiKey": "fjVJqBSmuyAX...",
        "lang": "kor",
        "success": function() {
            console.log("초기화 성공"); },
        "fail": function(error) {
            console.log(error.code + " : " + error.message);
        } 
    
    });


    const Yeskey = class {
        constructor(id) {
            if(typeof this[id] == 'undefined') throw '>>>naya ['+id+'] 메소드 추가해주세요.';
            else this[id]();
        }

        issue() {
            console.log('인증서 발급/재발급 시작');
        }

        renew() {
            console.log('인증서 갱신발급 시작');
        }
    };

    let btn  = $(".btn").on('click', function(event) {
        new Yeskey(this.id);
    });



</script>
</body>
</html>
<!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>Document</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://t-4user.yeskey.or.kr/v1/fincert.js?dt=20200814"></script>
</head>
<body>

    <button id="issue" class="btn">인증서발급/재발급</button>    
    <button id="renew" class="btn">인증서 갱신발급</button>    
    <button id="renew2" class="btn">인ㅅㄴㅅ</button>    
    
<script>
   
    const Yeskey = class {
        constructor(id) {
            this.id = id;
        }

        exe() {
            if(typeof this[this.id] == 'undefined') throw '>>>naya ['+id+'] 메소드 추가해주세요.';
            else this[this.id]();
        }

        init() {
            FinCert.Sdk.init({
                "orgCode": "D200800001",
                "apiKey": "fjVJqBSmuyAX...",
                "lang": "kor",
                "success": function() {
                    console.log("초기화 성공"); },
                "fail": function(error) {
                    console.log(error.code + " : " + error.message);
                } 
            
            });
        }

        issue() {
            console.log('인증서 발급/재발급 시작');
        }

        renew() {
            console.log('인증서 갱신발급 시작');
        }
    };


    let btn  = $(".btn").on('click', function(event) {
        new Yeskey(this.id).exe();
    });

    let yk = new Yeskey();
    yk.init();

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