Java/JSP

Javascript 복습 - 2

성엽이 2019. 6. 21. 12:46

Javascript 복습 - 2


   <script>
      var plusTen = function() {
        var num = prompt("숫자를 입력해주세요.""");
        alert(Number(num) + 10);
      };
      //   alert(typeof plusTen);
      //   plusTen();
 
      //함수를 변수처럼 대입이 가능하다.
      var add = plusTen;
      //   add();
      //   prompt("숫자입력", 1);
 
      //함수는 재정의가 가능하다.
      // 결과: 30
      function plusTen() {
        alert(10);
      }
      function plusTen() {
        alert(30);
      }
      plusTen();
 
      // native code : 선언적 함수.
      alert(prompt);
 
      function printTen(x) {
        return x + 10;
      }
 
      console.log(printTen(3));
 
      function printAB() {
        alert("a");
        alert("b");
        return "c";
      }
      var out = printAB();
      alert(out);
      // A 만 출력됨, 에러는 안뜸
      alert("A""B");
 
      // 10 만 출력됨, 에러는 안뜸
      function printX(x) {
        console.log(x);
      }
      printX(102030);
      function add() {
        var output = "";
        alert(typeof arguments);
        alert(arguments.length);
 
        for (var i = 0; i < arguments.length; i++) {
          output += arguments[i] + " ";
        }
        return output;
      }
      var out = add(123);
      alert(out);
 
      // 내부함수 실행코드
      function outerFunction() {
        var a = 10;
        var b = 20;
 
        function innerFunction() {
          var b = 30;
          console.log("a: ", a);
          console.log("b: ", b);
          console.log("내부함수 실행완료");
        }
 
        innerFunction();
        console.log("외부함수 실행완료");
        // return innerFunction;
      }
 
      outerFunction();
      // 내부함수기 때문에 에러가 뜬다.
      innerFunction();
 
      //   자기 호출 함수
      (function() {
        console.log("자기호출함수");
      })();
 
      //   매개변수로 한글도 된다!
      (function(이름) {
        console.log(`입력받은 이름은 ${이름} 입니다.`);
      })("홍길동");
 
      (function(name, age) {
        console.log(`입력받은 이름은 ${name}, 나이는 ${age} 입니다.`);
      })("홍길동""30");
 
      // 콜백함수 : 매개변수를 함수로 전달
      function func(call) {
        console.log(typeof call);
        call();
      }
      function myCall() {
        console.log("myCall 호출됨");
      }
      func(myCall);
 
      var aFunc = function() {
        console.log("익명함수 호출됨");
      };
 
      func(aFunc);
      //   확인 : true / 취소 : false
      alert(confirm("정말로 삭제 하시겠습니까?"));
 
      // question : string , yes : function , no : function
      function ask(question, yes, no) {
        if (confirm(question) == true) {
          yes(); // showYes 함수 호출
        } else {
          no(); // showNo 함수 호출
        }
      }
 
      function showYes() {
        alert("동의했습니다.");
      }
 
      function showNo() {
        alert("취소했습니다.");
      }
 
      ask("동의하십니까?", showYes, showNo);
    </script>
cs