WEB/HTML+CSS+JS

JavaScript ๋‚ด์šฉ์ •๋ฆฌ - 4

kite707 2021. 1. 4.

ํ•จ์ˆ˜ 

๋‹ค๋ฅธ ์–ธ์–ด์™€ ํ™œ์šฉ์ด ์œ ์‚ฌํ•ด์„œ ์‹ค์Šต๋งŒ ํ•ด๋ณด๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ๋‹ค.

<!DOCTYPE html>
<html>
<h1>Function</h1>
    <h2>Basic</h2>
    <ul>
      <script>
        function two(){
          document.write('<li>2-1</li>');
          document.write('<li>2-2</li>');
        }
        document.write('<li>1</li>');
        two();
        document.write('<li>3</li>');
        two();
      </script>
    </ul>
    <h2>Parameter & Argument</h2>
    <script>
      function onePlusOne(){
        document.write(1+1+'<br>');
      }
      onePlusOne();
      function sum(left, right){
        document.write(left+right+'<br>');
      }
      sum(2,3); // 5
      sum(3,4); // 7
    </script>
    <h2>Return</h2>
      </body>
</html>

ํ•จ์ˆ˜ ์ •์˜ ๋ฐฉ๋ฒ•

function(๋งค๊ฐœ๋ณ€์ˆ˜1, ๋งค๊ฐœ๋ณ€์ˆ˜2 ......){
 	ํ•จ์ˆ˜ ๋‚ด์šฉ;
    }

ํ•จ์ˆ˜์˜ ๋ฆฌํ„ดํ˜•์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž.

<!DOCTYPE html>
<html>
<h1>Function</h1>
<h2>Basic</h2>
<ul>
  <script>
    function two(){
      document.write('<li>2-1</li>');
      document.write('<li>2-2</li>');
    }
    document.write('<li>1</li>');
    two();
    document.write('<li>3</li>');
    two();
  </script>
</ul>
<h2>Parameter & Argument</h2>
<script>
  function onePlusOne(){
    document.write(1+1+'<br>');
  }
  onePlusOne();
  function sum(left, right){
    document.write(left+right+'<br>');
  }
  function sumColorRed(left, right){
    // document.write('<div style="color:red">'+(left+right)+'</div><br>');
    document.write('<div style="color:red">'+left+right+'</div><br>');
  }
  sum(2,3); // 5
  sumColorRed(2,3); // 23  //์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฌธ์ž์™€ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋ฉด ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๊ฐ„์ฃผํ•˜๊ธฐ ๋•Œ๋ฌธ์—
  //+๋ฅผ ๋ง์…ˆ์ด ์•„๋‹Œ ๋ฌธ์ž๊ฒฐํ•ฉ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ 23์ด ๋‚˜์˜จ๋‹ค.
  sum(3,4); // 7
</script>
<h2>Return</h2>
<script>
  function sum2(left, right){
    return left+right;
  }
  document.write(sum2(2,3)+'<br>');
  document.write('<div style="color:red">'+sum2(2,3)+'</div>');
  document.write('<div style="font-size:3rem;">'+sum2(2,3)+'</div>');
</script>
      </body>
</html>

์ด ์ฝ”๋“œ์—์„œ๋Š” ์ฃผ์„์ฒ˜๋ฆฌํ•œ ๋ถ€๋ถ„๋งŒ ๋ˆˆ์—ฌ๊ฒจ ๋ณด๋ฉด ๋  ๋“ฏ ํ•˜๋‹ค. ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฌธ์ž์™€ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋ฉด ์ˆซ์ž๋ฅผ ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๊ฐ„์ฃผํ•˜๊ธฐ ๋•Œ๋ฌธ์— +๋ฅผ ๋ง์…ˆ์ด ์•„๋‹Œ ๋ฌธ์ž๊ฒฐํ•ฉ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

๊ฐ์ฒด 

์ด๋ฒˆ์—๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ , ๋˜ ๊ทธ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค.

 

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• + ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

  var coworkers={
        "programmer":"egoing",
        "designer":"leezche"
      };

๋ฐ์ดํ„ฐ๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.

      coworkers.bookkeeper="duru"; //coworkers๋Š” ๊ฐ์ฒด ์ด๋ฆ„
      coworkers["data scientist"]="taeho"; //๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์žˆ์„ ๋•Œ

๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•

      document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
      document.write("data scientist : "+coworkers["data scientist"]+"<br>");

์ฆ‰ ์ „์ฒด ์ฝ”๋“œ์™€ ์‹คํ–‰๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋œ๋‹ค.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <h1>Object</h1>
    <h2>Create</h2>
    <script>
      var coworkers={
        "programmer":"egoing",
        "designer":"leezche"
      };
      document.write("programmer : "+coworkers.programmer+"<br>");
      document.write("designer : "+coworkers.designer+"<br>");
      coworkers.bookkeeper="duru";
      coworkers["data scientist"]="taeho";
      document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
      document.write("data scientist : "+coworkers["data scientist"]+"<br>");
    </script>
  </body>
</html>

 

์ด์ œ ๊ฐ์ฒด์™€ ๋ฐ˜๋ณต๋ฌธ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด๋ณด์ž. ์‚ฌ์šฉ๋ฒ•์€ ํŒŒ์ด์ฌ๊ณผ ์ƒ๋‹นํžˆ ์œ ์‚ฌํ•˜๋‹ค. ์šฐ์„  ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ–ˆ๋˜ ๋ถ€๋ถ„์„ ๋ณด๋ฉด

key : value ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ์–ธํ•œ๋‹ค.

var ๋ณ€์ˆ˜์ด๋ฆ„{
	key1 : value1
	key2 : value2
}

์ด๋Ÿฌํ•œ key value๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•ด๋ณด์ž.

      for(var key in coworkers){
        document.write(key+"<br>");
      }

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด key๊ฐ’๋งŒ์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ ์ด๊ฒƒ์„ ์ด์šฉํ•ด ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <h1>Object</h1>
    <h2>Create</h2>
    <script>
      var coworkers={
        "programmer":"egoing",
        "designer":"leezche"
      };
      document.write("programmer : "+coworkers.programmer+"<br>");
      document.write("designer : "+coworkers.designer+"<br>");
      coworkers.bookkeeper="duru";
      coworkers["data scientist"]="taeho";
      document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
      document.write("data scientist : "+coworkers["data scientist"]+"<br>");
    </script>
    <h1>Iterate</h1>
    <script>
      for(var key in coworkers){
        document.write(key+" : "+coworkers[key]+"<br>");
      }
    </script>
  </body>
</html>

์‹คํ–‰ ํ™”๋ฉด๋„ ์›๋ž˜ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ ์ด๊ฒƒ์„ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <h1>Object</h1>
    <h2>Create</h2>
    <script>
      var coworkers={
        "programmer":"egoing",
        "designer":"leezche"
      };
      document.write("programmer : "+coworkers.programmer+"<br>");
      document.write("designer : "+coworkers.designer+"<br>");
      coworkers.bookkeeper="duru";
      coworkers["data scientist"]="taeho";
      document.write("bookkeeper : "+coworkers.bookkeeper+"<br>");
      document.write("data scientist : "+coworkers["data scientist"]+"<br>");
    </script>
    <h1>Iterate</h1>
    <script>
      for(var key in coworkers){
        document.write(key+" : "+coworkers[key]+"<br>");
      }
    </script>
    <h2>Property & Method</h2>
    <script>
      coworkers.showAll = function(){
        for(var key in this){
          document.write(key+' : '+this[key]+'<br>');
        }
      }
      coworkers.showAll();
    </script>
  </body>
</html>

์ด ์ฝ”๋“œ์—์„œ this๋Š” ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋œ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋‹ค๋งŒ ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด 

์ด๋ ‡๊ฒŒ ๋งจ ๋ฐ‘์ค„ ๋ถ€๋ถ„๋„ ํ™”๋ฉด์— ๋‚˜์˜ค๋Š”๋ฐ ์ด๊ฒƒ์€ if๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ง€์šธ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

 

๋ฐฐ์šด ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ์•ž์„œ ๋งŒ๋“ค์—ˆ๋˜ ์ฝ”๋“œ๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<!doctype html>
<html>
<head>
  <title>WEB1 - JavaScript</title>
  <meta charset="utf-8">
  <body>
  <script>
  function LinksSetColor(color){
    var alist = document.querySelectorAll('a');
    var i = 0;
    while(i < alist.length){
      alist[i].style.color = color;
      i = i + 1;
    }
  }
  var Links = {
    setColor:function(color){
      var alist = document.querySelectorAll('a');
      var i = 0;
      while(i < alist.length){
        alist[i].style.color = color;
        i = i + 1;
      }
    }
  }
  function BodySetColor(color){
    document.querySelector('body').style.color = color;
  }
  function BodySetBackgroundColor(color){
    document.querySelector('body').style.backgroundColor = color;
  }
  var Body = { //๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•œ ๋ถ€๋ถ„
    setColor:function (color){
      document.querySelector('body').style.color = color;
    },
    setBackgroundColor:function (color){
      document.querySelector('body').style.backgroundColor = color;
    }
  }
  function nightDayHandler(self){
    var target = document.querySelector('body');
  }
  </script>
<h2>JavaScript</h2>
  <p>
    JavaScript (/หˆdส’ษ‘หvษ™หŒskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
  </p>
</body>
</html>

๋˜ ์ด๊ณณ์—์„œ script๋ถ€๋ถ„๋งŒ์„ ๋ณต์‚ฌํ•ด์„œ ํŒŒ์ผ๋ช….jsํŒŒ์ผ๋กœ ์ €์žฅํ•œ ๋’ค 

<script src="ํŒŒ์ผ๋ช….js"></script>

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์œ„์˜ <script></script>๋กœ ๋ฌถ์ธ ๋ถ€๋ถ„์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํŒŒ์ผ์˜ ์œ ์ง€๋ณด์ˆ˜์— ๋ณด๋‹ค ํŽธํ•ด์ง„๋‹ค.

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ”„๋ ˆ์ž„์›Œํฌ 

https://webclub.tistory.com/458

 

ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฐจ์ด์ 

Framework Vs Library ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ •ํ™•ํ•œ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋Œ€์ค‘ ์•Œ๊ฒƒ ๊ฐ™์ง€๋งŒ ์ •ํ™•ํžˆ ์–ด๋– ํ•œ ์ฐจ์ด์ ์ด ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์„์ง€๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค. ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋‹จ์ง€ ๋ฏธ๋ฆฌ ๋งŒ

webclub.tistory.com

UI vs API 

https://velog.io/@rimu/UI-API-%EC%97%90-%EB%8C%80%ED%95%9C-%EC%A0%95%EB%A6%AC

 

[์›น ๊ธฐ์ดˆ] UI & API ์— ๋Œ€ํ•œ ์ •๋ฆฌ

๋งํฌ์ƒํ™œ์ฝ”๋”ฉ javascript ๊ฐ•์˜ ์ค‘ ui & api ์— ๋Œ€ํ•œ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๊ทธ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด๋ดค์Šต๋‹ˆ๋‹ค. UI๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(์ดํ•˜ ์•ฑ)์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž(user) ๊ฐ€ ์‹œ์Šคํ…œ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š”

velog.io

ํ•ด๋‹น ๋ถ€๋ถ„๋“ค์€ ์œ„ ๊ธ€๋“ค์— ์ž์„ธํ•œ ์„ค๋ช…์ด ๋‚˜์™€์žˆ๋‹ค.

๋Œ“๊ธ€