๐ก ๋์ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํต์ฌ ๊ฐ์ด๋๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํฉ๋๋ค.
# 2.1 ํ์ดํ ํจ์
// ์ผ๋ฐ ํจ์
const greeting = function(name) {
return "hello " + name;
}
// ํ์ดํ ํจ์
const greeting = (name) => {
return `hello ${name}`;
}
// ํ์ดํ ํจ์ (๋งค๊ฐ ๋ณ์๊ฐ ํ๋ ์ผ ๊ฒฝ์ฐ, ๊ดํธ ์๋ต)
const greeting = name => {
return `hello ${name}`;
}
// ํ์ดํ ํจ์ (๋งค๊ฐ ๋ณ์๊ฐ ์์ ๊ฒฝ์ฐ, ๊ดํธ ํ์)
const greeting = () => {
return `hello`;
}
์ฐธ๊ณ : ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด
Template Literals
๋ฐฑํฑ์ ์ฌ์ฉํ์ฌ string + ๋ณ์์ ํํ๋ฅผ ํํํ๋ ๋ฐฉ๋ฒ
const sayHello = (name = ‘Newstar’) => `Hello ${name}!`
# 2.2 ์์์ ๋ฐํ
Arrow Function์ {}
๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด ํ์ดํ ๋ค์ ๋์ค๋ ๋ด์ฉ์ ๊ธฐ๋ณธ์ ์ผ๋ก return
์ ํจ์ถ.
๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ฐํ ์ ๊ฐ์ฒด๋ฅผ ()
๋ก ๊ฐ์ธ์ฃผ์ด ๋ฐํํ๊ณ ์ ํ๋ ๊ฐ์ด ๊ฐ์ฒด์์ ํํํด์ฃผ์ด์ผ ํ๋ค.
// string ๋ฐํ
const greeting = (name) => `hello ${name}`;
// object literal ๋ฐํ
const race = "100m dash";
const runners = ["Usain Bolt", "Justin Gatlin", "Asafa Powell"];
const results = runners.map((runner, i) => ({name: runner, race, place: i + 1}));
console.log(results);
์ฐธ๊ณ : map
Array.map()
Calls a defined callback function on each element of an array, and returns an array that contains the results.
๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ ์๋ ์ฝ๋ฐฑํจ์๋ฅผ ํธ์ถํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํ ๋ฐฐ์ด์ ๋ฆฌํดํ๋ค.
// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
const result = students.map((student) => student.score);
console.log(result);
}
์ ์ฝ๋ฐฑํจ์์ ์ธ์๋ฅผ student
๋์ value
, item
๋ฑ์ผ๋ก ๋์ ํ ์๋ ์์ง๋ง ์ง๊ด์ ์ด์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ์ต๋ํ ๊ทธ ์๋ฏธ๋ฅผ ์ง๊ด์ ์ผ๋ก ์์๋ณผ ์ ์๋๋ก ๋ค์ด๋ฐํ๋ ๊ฒ์ด ์ค์
# 2.3 ํ์ดํ ํจ์๋ ์ต๋ช ํจ์
ํ์ดํ ํจ์๋ ํญ์ ์ด๋ฆ์ด ์๋ ์ต๋ช ํจ์์ด๋ฉฐ, ์ฐธ์กฐ๊ฐ ํ์ํ ๊ฒฝ์ฐ ํจ์๋ฅผ ๋ณ์์ ํ ๋นํ์ฌ ์ฌ์ฉ
const greeting = name => `hello ${name}`;
greeting("Newstar");
# 2.4 ํ์ดํ ํจ์์ this ํค์๋
ํ์ดํ ํจ์ ๋ด์ this
๋ ์์ ์ค์ฝํ์ this
๋ฅผ ์์ ๋ฐ๋๋ค. ์ด๋ฅผ Lexical this๋ผ ํ๋ค.
์๋์ ๊ฐ์ ๊ฒฝ์ฐ ์ ์ฉํ๊ฒ ํ์ฉ ๊ฐ๋ฅํ๋ค.
const box = document.querySelector(".box");
// click ์ด๋ฒคํธ ํธ๋ค๋ฌ ๋ฑ๋ก
box.addEventListener("click", function() {
// div์ opening ํด๋์ค ํ ๊ธ
this.classList.toggle("opening"); // this == box
setTimeout(() => { // ์ฌ๊ธฐ์ ์ผ๋ฐ ํจ์ ์ฌ์ฉํ ๊ฒฝ์ฐ, ํจ์ ๋ด๋ถ์ this๊ฐ ์ ์ญ ๊ฐ์ฒด์ธ window๋ฅผ ๊ฐ๋ฆฌ์ผ ์ค๋ฅ ๋ฐ์
// ํด๋์ค ๋ค์ ํ ๊ธ
this.classList.toggle("opening"); // this == box
}, 500);
})
์ฐธ๊ณ : addEventListener ํจ์ ๋ด์ this
addEventListener ํจ์ ๋ด์ this
๐ ์ฐธ๊ณ ๋งํฌ
- ํ์ดํ ํจ์๋ก ์ ์๋
addEventListener
ํจ์์ ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์this
๋ ์ ์ญ ๊ฐ์ฒด Window๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// Bad
var button = document.getElementById('myButton');
button.addEventListener('click', () => {
console.log(this === window); // => true
this.innerHTML = 'Clicked button';
});
- ์ผ๋ฐ ํจ์๋ก ์ ์๋
addEventListener
ํจ์์ ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์this
๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋์ ๋ฐ์ธ๋ฉ๋ ์์(currentTarget)๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// Good
var button = document.getElementById('myButton');
button.addEventListener('click', function() {
console.log(this === button); // => true
this.innerHTML = 'Clicked button';
});
์ฐธ๊ณ : ์ ์ญ๊ฐ์ฒด(Global Object)
์ ์ญ๊ฐ์ฒด(Global Object)
์ ์ญ๊ฐ์ฒด(Global Object)๋ ๋ชจ๋ ๊ฐ์ฒด์ ์ ์ผํ ์ต์์ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ฉฐ ์ผ๋ฐ์ ์ผ๋ก Browser-side์์๋ window
, Server-side(Node.js)์์๋ global
๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค.
// in browser console
this === window // true
// in Terminal node
this === global // true
# 2.5 ํ์ดํ ํจ์๋ฅผ ํผํด์ผ ํ๋ ๊ฒฝ์ฐ
๋ฌธ์ ์ํฉ 1.
const button = document.querySelector('btn');
button.addEventListener('click', () => {
// ์ค๋ฅ : ์ฌ๊ธฐ์ this๋ Window ๊ฐ์ฒด
this.classList.toggle('on');
})
๋ฌธ์ ์ํฉ 2.
const person1 = {
age: 10,
grow: function() {
this.age++;
console.log(this.age);
}
}
person1.grow(); // 11
const person2 = {
age: 10,
grow: () => {
this.age++;
console.log(this.age);
}
}
person2.grow(); // NaN
๋ฌธ์ ์ํฉ 3.
// ๋ฌธ์ ์ํฉ
const showWinner = () => {
const winner = arguments[0];
console.log(`${winner} was the winner`);
}
showWinner("Usain", "Justin", "Asafa"); // arguments is not defined
// ํด๊ฒฐ ๋ฐฉ๋ฒ 1 : ์ผ๋ฐ ํจ์ ์ฌ์ฉ
const showWinner = function() {
const winner = arguments[0];
console.log(`${winner} was the winner`);
}
showWinner("Usain", "Justin", "Asafa"); // Usain was the winner
// ํด๊ฒฐ ๋ฐฉ๋ฒ 2 : ์คํ๋ ๋ ๋ฌธ๋ฒ ์ฌ์ฉ
const showWinner = (...args) => {
const winner = args[0];
console.log(`${winner} was the winner`);
}
showWinner("Usain", "Justin", "Asafa"); // Usain was the winner
์ฐธ๊ณ : spread
spread
spread ๋ผ๋ ๋จ์ด๊ฐ ๊ฐ์ง๊ณ ์๋ ์๋ฏธ๋ ํผ์น๋ค, ํผ๋จ๋ฆฌ๋ค ์ด๋ค. ์ด ๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ฉด, ๊ฐ์ฒด ํน์ ๋ฐฐ์ด์ ํผ์น ์ ์๋ค. ์๋์ ๊ฐ์ด ๊ธฐ์กด์ ๊ฐ์ฒด๋ฅผ ๊ฑด๋๋ฆฌ์ง ์๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผํ๋ ๊ฒฝ์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
const slime = {
name: '์ฌ๋ผ์'
};
const cuteSlime = {
...slime,
attribute: 'cute'
};
const purpleCuteSlime = {
...cuteSlime,
color: 'purple'
};
console.log(slime); // { name: "์ฌ๋ผ์" }
console.log(cuteSlime); // { attribute: "cute", name: "์ฌ๋ผ์" }
console.log(purpleCuteSlime); // { attribute: "cute", name: "์ฌ๋ผ์", color: "purple" }
๋ง์ฐฌ๊ฐ์ง๋ก ๋ฐฐ์ด์์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
const animals = ['๊ฐ', '๊ณ ์์ด', '์ฐธ์'];
const anotherAnimals = [...animals, '๋น๋๊ธฐ'];
console.log(animals); // ['๊ฐ', '๊ณ ์์ด', '์ฐธ์']
console.log(anotherAnimals); // ['๊ฐ', '๊ณ ์์ด', '์ฐธ์', "๋น๋๊ธฐ"]
๋ฐฐ์ด์์ spread ์ฐ์ฐ์๋ฅผ ์ฌ๋ฌ๋ฒ ์ฌ์ฉ ํ ์๋ ์๋ค.
const numbers = [1, 2, 3, 4, 5];
const spreadNumbers = [...numbers, 1000, ...numbers];
console.log(spreadNumbers); // [1, 2, 3, 4, 5, 1000, 1, 2, 3, 4, 5]
2.6 QUIZ
Q 2.1. ๋ค์ ์ค ํ์ดํ ํจ์๋ฅผ ๋ฌธ๋ฒ์ ๋ง๊ฒ ํ์ฉํ ์๋?
let arr = [1, 2, 3];
// a)
let func = arr.map(n -> n + 1);
// b)
let func = arr.map(n => n + 1);
// b)
let func = arr.map(n ~> n + 1);
- ๋ต : A. b
Q 2.2. ๋ค์ ์ฝ๋์ ์ฌ๋ฐ๋ฅธ ์ถ๋ ฅ์?
const person = {
age: 10,
grow: () => {
this.age++;
}
}
person.grow();
console.log(person.age);
- ๋ต : A. 10
- ์ ์ฝ๋์์
this
๋ ์ ์ญ๊ฐ์ฒด Window๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋กperson.age
์ ๊ฐ์ ๋ณํจ ์์ด 10์ด๋ค.
- ์ ์ฝ๋์์
Q 2.3. ํ์ดํ ํจ์ ๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ๋ค์ ์ฝ๋๋ฅผ ๋ฆฌํฉํฐ๋งํ์.
function(arg) {
console.log(arg);
}
- ๋ต
const arrFunc = arg => { console.log(arg) }
'Tech > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฌ๋ค์ผ ๊ฐค๋ฌ๋ฆฌ ๊ตฌํ / Gallery with Thumbnail Slider / Swiper - Thumbs gallery (0) | 2022.01.11 |
---|---|
๋ชจ๋ฐ์ผ ๋๋ฐ์ด์ค ๋ถ๊ธฐ / Javascript ๋ฐ์ํ ์ฒ๋ฆฌ (0) | 2022.01.10 |
Global Object (0) | 2021.08.06 |
rest์ spread (0) | 2021.07.22 |
Prompt ๊ธฐ๋ฅ์ผ๋ก Callback๊ณผ Promise ๋น๊ตํ๊ธฐ (0) | 2021.04.22 |