# ๐ฆ ๋จํ์ผํ๋นตํ์กฐ
## ์กฐ์ ์๊ฐ
#### ๊นํ๋ธ ๋ง~ ํํด์ฃผ์ธ์~ ๐๐
- ๋จํ์กฐ ๋์ฅ+๋ง์ค์ฝํธ : [์๊ทํ](https://github.com/lktgt15)
- ๋จํ์กฐ ์ซ๋ค๊ตฌ : [์ด์ฐ์ฃผ](https://github.com/joylish)
## ๐ฅ ์ด๋ฒ์ฃผ ์์
๋ฌผ์ ์ ์ํฉ๋๋ค~
### 01. ๋ถ์นดํ
- ๐ถ ๋จํ
[์ง๊ธ ๋ฐ๋ก ๋ณด๋ฌ๊ฐ๊ธฐ๐โโ๏ธ](https://codepen.io/lktgt15/pen/abBjaxZ)

- ๐ฐ ์ฐ์ฃผ
[์ง๊ธ ๋ฐ๋ก ๋ณด๋ฌ๊ฐ๊ธฐ๐โโ๏ธ](https://codepen.io/joylish/pen/PobaMyB)

### 02. ๋ก๋
- ๐ถ ๋จํ
[์ง๊ธ ๋ฐ๋ก ๋ณด๋ฌ๊ฐ๊ธฐ๐โโ๏ธ](https://codepen.io/joylish/pen/gOLjjVg)


- ๐ฐ ์ฐ์ฃผ
[์ง๊ธ ๋ฐ๋ก ๋ณด๋ฌ๊ฐ๊ธฐ๐โโ๏ธ](https://codepen.io/joylish/pen/BaQPLyw)

## ๐ ์ด๋ฒ์ฃผ ๊ณต์ ํ ๊ฒ
- [codepen](https://codepen.io/)์ ์ฐ์๋ฉด ์๋ก ๊ณต์ ํ ์ ์์ด์!
- [๊นจ์ ์ง์ง์๋] ๊ทํ๋์ด ์๊ณ ๋ฆฌ์ฆ ๊ณต๋ถ ์๋ฃ๋ฅผ ๊ณต์ ํด์ฃผ์
จ์ด์! ์ผํ๋์ฅ๋์ ๐น ์๊ณ ๋ฆฌ์ฆ ๋๋ง์ DMW ๐น
## ๐ ์ด๋ฒ์ฃผ ๊ณต๋ถ ์ ๋ฆฌ
### DOM ( Document Object Model )
- ๋ฌธ์์ ๋ด์ฉ์ ๊ฐ์ฒด๋ก ํํ
- `<tag>` : ๊ฐ์ฒด๋ฅผ ํํ, ๊ณ์ธต๊ตฌ์กฐ๋ฅผ ๊ฐ์ง, ๋
ธ๋๋ผ๊ณ ์๊ฐ, ๋
ธ๋์ ํ์
(Element,Text ๋ฑ),์ด๋ฆ,๋ฐธ๋ฅ๋ฑ์ ์์๋ผ ์ ์์
- Element๋ ๋ฐธ๋ฅ๊ฐ ์๊ณ name์ด ์์
- TextNode๋ name์ด ์๊ณ ๋ฐธ๋ฅ๊ฐ ์์
`<h1>(E) => <TextNode>(T)` ์ผ ๋ TextNode๋ฅผ ์ฐพ์ผ๋ ค๋ฉด h1์ ์ฐพ์์ผํจ. ์ด๋ฆ์ผ๋ก ์ฐพ๊ธฐ, ๋ฃจํธ๋ถํฐ ํ์ ๋ฑ ๋ฐฉ๋ฒ์ด ๋ค์ํจ
1. Browser๋ html ํ์ฑ
2. tag(element) ๊ฐ์ฒด๋ก ์์ฑ
3. Tree ๊ตฌ์ฑ
4. Tree๋๋ก ํ๋ฉด์ ๊ทธ๋ฆฐ๋ค.
*DOM TREE*๋ฅผ ์กฐ์ํ๋ฉด ํ๋ฉด์ด ๋ณ๊ฒฝ๋๋ค. => Dynamic HTML ํจ๊ณผ๋ฅผ ์ค๋ค.
### ๊ฐ๋ณ์ธ์ํจ์
```JavaScript
<script type="text/javascript">
function myFunc(num1,num2){
console.log(num1+"/"+num2);
}
/* myFunc(1); ์ธ์ ๋ถ์กฑ : ์๋ฌ์๋จ
myFunc(2,3);
myFunc(4,5,6); ์ธ์ ์ด๊ณผ : ์๋ฌ์๋จ */
function sumAll(){
let ret = 0;
for(let i=0;i<arguments.length;i++){
ret += arguments[i];
}
return ret;
}
console.log(sumAll(10));
console.log(sumAll(10,20));
console.log(sumAll(10,20,30));
</script>
```
### ํจ์ ์ด๋ฆ ๊ฒน์นจ, ํจ์ ์ธ์๋ก ๋๊น, ํจ์ ๋ฆฌํด
```HTML5
<script type="text/javascript">
function myFunc(num1,num2){
console.log(num1+"/"+num2);
}
/* myFunc(1); ์ธ์ ๋ถ์กฑ : ์๋ฌ์๋จ
myFunc(2,3);
myFunc(4,5,6); ์ธ์ ์ด๊ณผ : ์๋ฌ์๋จ */
// ๊ฐ๋ณ์ธ์ํจ์
function sumAll(){
let ret = 0;
for(let i=0;i<arguments.length;i++){
ret += arguments[i];
}
return ret;
}
console.log(sumAll(10));
console.log(sumAll(10,20));
console.log(sumAll(10,20,30));
function big(value1,value2){
return value1>value2?value1:value2;
}
console.log(big(100,200));
</script>
<script type="text/javascript" src="./js/util.js"></script>
```
- ์คํฌ๋ฆฝํธ์์ ํจ์ ์ด๋ฆ์ด ๊ฒน์น ์ ์๋ค.
```HTML
<script type="text/javascript">
function myFunc(num1,num2){
console.log(num1+"/"+num2);
}
/* myFunc(1); ์ธ์ ๋ถ์กฑ : ์๋ฌ์๋จ
myFunc(2,3);
myFunc(4,5,6); ์ธ์ ์ด๊ณผ : ์๋ฌ์๋จ */
// ๊ฐ๋ณ์ธ์ํจ์
function sumAll(){
let ret = 0;
for(let i=0;i<arguments.length;i++){
ret += arguments[i];
}
return ret;
}
console.log(sumAll(10));
console.log(sumAll(10,20));
console.log(sumAll(10,20,30));
function process(){
function big(value1,value2){
return value1>value2?value1:value2;
}
console.log(big(100,200));
}
process();
console.log("outer : "+big(100,200));
</script>
<script type="text/javascript" src="./js/util.js"></script>
```
- ํจ์ ์์ ํจ์๋ฅผ ๋ง๋ค์ด์ script๊ฐ ์ถฉ๋์ ๋ง์ ์ ์๋ค.
```HTML
<script type="text/javascript">
function display(viewHandler){
let msg = "hello World!";
// msg ํ๋ฉด์ ๋ํ๋ด๊ธฐ : ์ํฉ์ ๋ฐ๋ผ ๋ค๋ฆ.
// ์ํฉ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ ์ฝ๋ (==> ํจ์)๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ์คํ
viewHandler(msg);
}
display(function(msg){
alert(msg);
});
</script>
</head>
<body>
<div id="greeting"></div>
<script type="text/javascript">
display(function(msg){
document.getElementById("greeting").innerText = msg;
});
</script>
</body>
```
- ํจ์๋ฅผ ์ธ์๋ก ๋๊ธฐ๋ฉด ๋ค์ํ ์ํฉ์ ๋์ํ ์ ์๋ค.
## ์์ฑ์ ํจ์
```JavaScript
// ์์ฑ์ํจ์ : ๊ท์น์ด ์๋ค.
// : ์ฌ์ฉํ๋ ์ชฝ์์ new ์ฐ์ฐ์์ ํจ๊ป ์ฌ์ฉ๋ ๋ ๋น๋ก์ ์์ฑ์ ํจ์๋ก ๋์ํ๋ค
function Person(name,age){
// ์์ฑ์ํจ์์์ this๋ ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// ๋์ ์์ฑ ์ถ๊ฐ
this.name = name;
this.age = age;
this.increaseAge = function(){
this.age++;
}
}
let p1 = new Person("๊น์ฌํ",26);
```
๋ค๋ง increaseAge๋ ๊ฐ์ฒด๋ง๋ค ๊ฐ์ง๊ณ ์์ ํ์๊ฐ ์๋ค.
```JavaScript
// ์์ฑ์ํจ์ : ๊ท์น์ด ์๋ค.
// : ์ฌ์ฉํ๋ ์ชฝ์์ new ์ฐ์ฐ์์ ํจ๊ป ์ฌ์ฉ๋ ๋ ๋น๋ก์ ์์ฑ์ ํจ์๋ก ๋์ํ๋ค
function Person(name,age){
// ์์ฑ์ํจ์์์ this๋ ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// ๋์ ์์ฑ ์ถ๊ฐ
this.name = name;
this.age = age;
}
// ํด๋์ค๋ฅผ ํ๋ด๋ธ๋ค.
// Person ํจ์ ๊ฐ์ฒด์ prototype
Person.prototype.increaseAge = function(){
this.age++;
};
let p1 = new Person("๊น์ฌํ",26);
let p2 = new Person("์ก์ค๊ธฐ",37);
console.log(p1.name+"/"+p1.age);
console.log(p2.name+"/"+p2.age);
```
์คํ ๊ฒฐ๊ณผ๋ ๊ฐ์ง๋ง ๋ฉ๋ชจ๋ฆฌ๊ตฌ์กฐ๋ ๋น์ฐํ ๋ค๋ฅด๊ฒ ๋๋ค.
### ๊ณ ์ฐจํจ์
ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๊ฑฐ๋ ๋ฆฌํดํ๋ ๊ฒ์ ๋งํ๋ค.
```JavaScript
let arr = [200,10,60,450,30];
arr.forEach(function(element){
console.log(element);
});
{
let res = arr.filter(function(element,index){
// ๊ฒฐ๊ณผ์ ํฌํจ์ํค๊ณ ์ถ์ผ๋ฉด true, ๋ฏธํฌํจ์ด๋ฉด false
// return element<100;
return index%2==1;
});
console.log(res);
}
{
let res = arr.map(function(element){
return element**2;
});
console.log(res);
console.log(arr);
}
{
let res = arr.filter(element => element < 100).map(element => element+50);
console.log(res);
}
```
## this
```JavaScript
// this : ์์ ์ด ์ํ ๊ฐ์ฒด, ๋ฏธ๋์ ์์ฑ๋ ๊ฐ์ฒด ์๊ธฐ ์์ ์ ๊ฐ๋ฆฌํค๋ ์๊ธฐ์ฐธ์กฐ๋ณ์
// : ํธ์ถ ์์ ์ ๊ฒฐ์ ๋จ
function sayHello(){
console.log(this);
console.log("Hello "+this.name);
}
sayHello(); // ์ผ๋ฐํจ์๋ก ํธ์ถ : this --> ์ ์ญ๊ฐ์ฒด(window)
let greeting = {
name : "๊น์ฌํ",
sayHi : sayHello,
}
greeting.sayHi(); // ๊ฐ์ฒด์ ๋ฉ์๋๋ก ํธ์ถ : this --> ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฐ์ฒด
new sayHello(); // ์์ฑ์ํจ์๋ก ํธ์ถ : this --> ์์ฑ๋๋ ๊ฐ์ฒด
```
## Event
- ํน๋ณํ ์ฌ๊ฑด, ์ํฉ
- UI์์์ ๋ฐ์ํ๋ ์ด๋ค Action, ์ํฉ
1. Event : ๋ฐ์ํ ์ด๋ฒคํธ ๊ด๋ จ ์ ๋ณด
2. Event Source : ์ด๋ฒคํธ ๋ฐ์ ๊ทผ์์ง(๋์)
3. Event Handler : ์ด๋ฒคํธ ๋ฐ์์ ์ฒ๋ฆฌ๊ธฐ
```HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function changeColor(){
console.log(this);
this.style.backgroundColor = 'pink';
console.log('change');
}
</script>
</head>
<body>
<!-- <h1 onclick="this.style.backgroundColor = 'pink';console.log('change');" style="background-color: grey;">์ต๊ฐ 6๋ฐ ํ์ดํ
!!!</h1>
-->
<h1 onclick="changeColor();" style="background-color: grey;">์ต๊ฐ 6๋ฐ ํ์ดํ
!!!</h1>
</body>
</html>
```
changeColor()๋ h1๊ฒ์ด ์๋๊ธฐ ๋๋ฌธ์ this๋ Window๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
```HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function changeColor(element){
console.log(element);
element.style.backgroundColor = 'pink';
console.log('change');
}
</script>
</head>
<body>
<!-- <h1 onclick="this.style.backgroundColor = 'pink';console.log('change');" style="background-color: grey;">์ต๊ฐ 6๋ฐ ํ์ดํ
!!!</h1>
-->
<h1 onclick="changeColor(this);" style="background-color: grey;">์ต๊ฐ 6๋ฐ ํ์ดํ
!!!</h1>
</body>
</html>
```