ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ


์ฒซ ๋ฒˆ์งธ๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ œ๊ฐ€ ES6 ๋ฌธ๋ฒ•์„ ์ ‘ํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ๋ณธ ๋…€์„ ์ค‘์— ํ•˜๋‚˜ ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋„ ๊ทธ๋žฌ๊ณ , c์–ธ์–ด๋‚˜ ์ž๋ฐ”์—์„œ๋„ ํ•จ์ˆ˜๋ผ๋˜์ง€ ๋ฐ˜๋ณต๋ฌธ๊ฐ™์€ ํ•˜๋‚˜์˜ ๋ฉ์–ด๋ฆฌ์˜ ํ”„๋กœ์„ธ์Šค๋Š” ๋Š˜ ์ค‘๊ด„ํ˜ธ๋กœ ๋ฌถ๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค.


๊ทธ๋Ÿฐ๋ฐ ES6์—์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด ๋“ฑ์žฅํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 



โ“์–ด๋–ป๊ฒŒ ์ƒ๊ฒจ๋จน์—ˆ๋Š”๊ฐ€??

๊ธฐ์กด์˜ ํ•จ์ˆ˜์ž‘์„ฑ๋ฐฉ์‹๊ณผ ๋น„๊ตํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์›น ์ƒ์—์„œ ํฌ๋กฌ ๊ฐœ๋ฐœ์ž๋„๊ตฌ๋ฅผ ์ด์šฉํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


html ํผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ํ˜ธ์ถœํ•ด์„œ ์ž‘์„ฑํ•˜์…”๋„ ๋˜๊ณ  ๊ทธ๋ƒฅ ํฌ๋กฌ ํŽ˜์ด์ง€์—์„œ ์ง์ ‘ ์ž‘์„ฑํ•ด๋„ ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค.


arrow.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
 
<head>
    <title>Arrow</title>
    <meta charset="utf-8" />
</head>
<body>
    <script src="arrow.js"></script>
</body>
 
</html>
cs


์ด๋Ÿฐ ์‹์œผ๋กœ arrow.js ํŒŒ์ผ์„ ํ˜ธ์ถœํ•˜๋Š” ์•„๋ฌด ๊ฒƒ๋„ ์—†๋Š” html ํผ์„ ๋งŒ๋“ค์–ด์„œ ์ง„ํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


arrow.js

1
2
3
4
function arrow() {
    console.log('arrow')    
}
arrow();
cs


์ด๊ฒŒ ์šฐ๋ฆฌ๊ฐ€ ๊ธฐ์กด์— ์•„๋Š” ํ•จ์ˆ˜์˜ ์ž‘์„ฑ๊ณผ ํ˜ธ์ถœ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํฌ๋กฌ ๊ฐœ๋ฐœ์ž๋„๊ตฌ์—์„œ ์ฝ˜์†”์„ ํ™•์ธํ•œ๋‹ค๋ฉด



์ด๋Ÿฐ์‹์œผ๋กœ ๋‚˜์˜ค๊ฒ ์ฃ ? ํ•จ์ˆ˜๋ฅผ ๋ง๋กœ์จ ์„ค๋ช…ํ•œ๋‹ค๋ฉด arrow๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด arrow๋ผ๋Š” ์ฝ˜์†”์ด ์ฐํžˆ๋Š”๋งค์šฐ ๊ฐ„๋‹จํ•œ ๋‚ด์šฉ์ด์ฃ  ํ™”์‚ดํ‘œํ•จ์ˆ˜๋กœ ๋ฐ”๊ฟ”์„œ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


1
2
3
4
var arrow = ()=>{
    console.log('arrow')
}
arrow()
cs

์ฐจ์ด์ ์€ ์ผ๋‹จ function ๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ํ•จ์ˆ˜๋ช…๋„ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•จ์ˆ˜๋ฅผ ์™ธ๋ถ€์—์„œ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์•„์„œ ํ˜ธ์ถœ์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ํ•จ์ˆ˜๊ฐ€ ์›Œ๋‚™ ์งง์•„์„œ ๊ฐ€๋…์„ฑ์˜ ์ฐจ์ด๊ฐ€ ์ „ํ˜€ ๋Š๊ปด์ง€์ง€ ์•Š์ง€๋งŒ ๋น„๊ต๋ฅผ ์œ„ํ•ด ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function boardSearchFunction(opt,word,callback) {
    let res = {'res':'null'}
    if(opt.opt=='์ž‘์„ฑ์ž'){
        models.User.findAll({
            where:{name:word.word}
        }).then(function(user){
            models.Board.findAll({
                where:{fk_userId: user[0].dataValues.id}
            }).then(function(user){
                callback(user)
            })
        }).catch(function(err){
            callback(res)
        })
    }
    else if(opt.opt=='์ œ๋ชฉ'){
        let i = 0
        let names=[]
        models.Board.findAll({
            where: {subject:word.word}
        }).then(function(board){
            if(board.length==0)
                callback(res)
            board.forEach(function(element){
                models.User.findAll({
                    where: {id: element.dataValues.fk_userId}
                }).then(function(user){
                    names[i] = {'name':user[0].dataValues.name}
                    i++
                    if(board.length==i)
                        callback(board,names)
                })
            })
        }).catch(function(err){
            callback(res)
        })
    }
    else
        callback(res)
}
cs


์ด๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š” ์ง€ ๋ณด๋ ค๊ณ  ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜ ์ž‘์„ฑ๋ฐฉ์‹์„ ๋ฐ”๊ฟจ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ๋ณด์ด๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ ์ž…๋‹ˆ๋‹ค.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var boardSearchFunction => (opt,word,callback) {
    let res = {'res':'null'}
    if(opt.opt=='์ž‘์„ฑ์ž'){
        models.User.findAll({
            where:{name:word.word}
        }).then((user)=>{
            models.Board.findAll({
                where:{fk_userId: user[0].dataValues.id}
            }).then((user)=>{
                callback(user)
            })
        }).catch((err)=>{
            callback(res)
        })
    }
    else if(opt.opt=='์ œ๋ชฉ'){
        let i = 0
        let names=[]
        models.Board.findAll({
            where: {subject:word.word}
        }).then((board)=>{
            if(board.length==0)
                callback(res)
            board.forEach((element)=>{
                models.User.findAll({
                    where: {id: element.dataValues.fk_userId}
                }).then((user)=>{
                    names[i] = {'name':user[0].dataValues.name}
                    i++
                    if(board.length==i)
                        callback(board,names)
                })
            })
        }).catch((err)=>{
            callback(res)
        })
    }
    else
        callback(res)
}
cs


์ผ๋‹จ ๊ธฐ๋ณธ์ ์œผ๋กœ function ๋‹จ์–ด ์ž์ฒด๊ฐ€ ๋ชจ๋‘ ์‚ฌ๋ผ์ง€๊ณ  ํ™”์‚ดํ‘œ =>  ๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—„์ฒญ๋‚˜๊ฒŒ ๊ฐ„๊ฒฐ ํ•ด์กŒ๋‹ค๊ณ  ํ•˜๊ธฐ๋Š” ์–ด๋ ค์šธ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•˜๋‚˜์˜ ์ด์œ ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 


์œ„์—์„œ ๋งํ–ˆ๋‹ค์‹œํ”ผ ํ™”์‚ดํ‘œํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ์ต๋ช…ํ•จ์ˆ˜ ๋กœ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋ช…์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๊ธฐ์— ์•„์ฃผ ์ข‹์ฃ . ์œ„์— ์ฝ”๋“œ๋„ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ™”์‚ดํ‘œํ•จ์ˆ˜๋กœ ๋ฐ”๊ฟจ๊ธฐ๋•Œ๋ฌธ์— ํŠน๋ณ„ํ•œ ์ œ์•ฝ์—†์ด ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.




๊ธฐ๋ณธ์ ์ธ ์„ ์–ธ์˜ ํ˜•ํƒœ๋ฅผ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ํŠน์ง•์—๋Š” ์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.


ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ํ‘œํ˜„(arrow function expression)์€ function ํ‘œํ˜„์— ๋น„ํ•ด ๊ตฌ๋ฌธ์ด ์งง๊ณ   ์ž์‹ ์˜ thisargumentssuper ๋˜๋Š” new.target์„ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ ์ต๋ช…์ž…๋‹ˆ๋‹ค. 

(์ถœ์ฒ˜ : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/%EC%95%A0%EB%A1%9C%EC%9A%B0_%ED%8E%91%EC%85%98)


์ด ๋ถ€๋ถ„์€ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ