๐ ์ดํํ ์ฝ๋ฉ ์๊ฐ 9hour
๐๐ผ -
๐๐ผ -
Homework
//๊ทธ๋ผ ์ด ๋ถ๋ถ ์ด๋ป๊ฒ await ํ์ฉํด์ ์ธ ์ ์์๊น?
const userStorage = new UserStorage();
const id = prompt('enter your id');
const password = prompt('enter your passrod');
userStorage
.loginUser(id,password)
.then(userStorage.getRoles)
.then(user => alert(`Hello ${user.name}, you have a ${user.role} role`);
.catch(console.log);
์ด๋ค ๊ฐ์ ์ ๋ฌ๋ฐ์ ์ฌ ๊ฑด์ง ์ ๋ฌ๋ฐ๋ ์ธ์ ์ ์ ๋ถ๋ถ + ํจ์ ์์ ๋์ํ๋ ์ฝ๋ ์์ฑํ๋ ๋ถ๋ถ
function doSomething() {
console.log('hello');; //์ ์ธ๋ง ํ ๊ฒ
}
doSomething(); //ํธ์ถ
๊ฐ์ ๋ฆฌํดํ๋ ํจ์
๊ณ์ฐ ํ ํน์ ๊ฐ ์ ๋ฌ, ๋ฆฌํด
//ํจ์ ์ ์ธ
function add(a,b) {
const sum = a+ b;
return sum;
}
//a์ b๋ผ๋ ํน์ ํ ์ธ์๊ฐ์ ๋ฐ์์์ const sum์ด๋ผ๋ ๋ณ์์ a,b ์ ์ฅ
//a,b ๊ฐ ๋ํ๋ค sum์ด๋ผ๋ ๋ณ์์ ํ ๋น
//a,b์ ๊ฐ ์ ๋ฌํด์ a,b๋ผ๋ ์ด๋ฆ์ผ๋ก ๊ฐ๊ฐ์ ๊ฐ์ ์ ๊ทผํ ์ ์๋ค
//์ฝ๋๋ธ๋ก ์ํ ๋ค return์ด๋ผ๋ ํค์๋ ์ด์ฉํด์ ๊ณ์ฐํ ๊ฐ ์ ๋ฌ
//ํจ์ ํธ์ถ
const result = add(1,2); //์ ์ํจ๊ณผ ๋์์ ๊ฐ ํ ๋น, ํจ์๋ฅผ ํธ์ถํ ๊ฒ
console.log(result);
function doSomething(add) {
console.log(add);; //์ ์ธ๋ง ํ ๊ฒ
}
doSomething(add); //ํจ์ ํธ์ถํ๋ฉด์ ์ธ์๋ก ํจ์ ์์ฒด๋ฅผ ์ ๋ฌ //f add(a,b) {...}
doSomething(add()); //์ด๋ ๊ฒ ํ๋ฉด ํจ์๊ฐ ๋ฐ๋ก ์คํ๋ผ๋ฒ๋ฆฐ๋ค //sum
//add ํธ์ถ๋ ๋ค์ ๋์จ ๋ง์ง๋ง์ผ๋ก ์ ๋ฌ๋ ๊ฐ์ด ์ ๋ฌ๋จ, ๋ฐ๋ผ์ ํจ์๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ด ์๋
//ํจ์ ์ ์ธ
function add(a,b) {
const sum = a+ b;
return sum;
}
function doSomething(add) {
console.log(add);;
const result = add(2,3);
console.log(result);
}
//ํจ์ ํธ์ถ
doSomething(add);
const addFun = add;
console.log(add);
addFun(1,2);
primitive ํ์ ๊ณผ object
๋ณ์
์ฒ๋ฆฌํด์ผ ํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ์ ์๋๋ก ๋์์ค๋ค. ๋ฐ์ดํฐ๋ฅผ ๋ด๊ณ ์์
๋ณ์๋ฅผ ํตํด์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ณ ์
๋ฐ์ดํธํ ์ ์๋ค
๋ด์ ์ ์๋ ๊ฐ์ฅ ์์ ๋จ์์ ๋ฐ์ดํฐ๋ฅผ primitive data type์ด๋ผ๊ณ ํ๋ค
๋ณ์์ ์ด๋ฆ์ ๋ก์ง๊ณผ ์ฐ๊ด๋ ์๋ฏธ์๋ ์ด๋ฆ์ผ๋ก ์ง๊ธฐ
number, string, boolean, null, undefined, symbol
let number = 2; //์ซ์๊ฐ ๋ณ์์ ํ ๋น
let number2 = number;
//number๋ผ๋ ๋ฐ์ดํฐ์ ๋ค์ด์๋ 2๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํด์ ํ ๋นํ๋ค
number2 = 3; //3์ ์
๋ฐ์ดํธ, number์๋ ์ํฅ์ ์ฃผ์ง ์๋๋ค
๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ number๋ผ๋ ๋ณ์ ์์ 2๋ผ๋ ๊ฐ, ์ซ์ ๋ฐ์ดํฐ๊ฐ ์ ์ฌ๋๋ค
๋ณ์๋ฅผ ์ ์ธํ๋ฉด ๋ฉ๋ชจ๋ฆฌ์ ๊ทธ ๋ณ์๋ฅผ ์ํ ๊ณต๊ฐ์ด ์๊ธด๋ค -> ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์ดํธ ์ฌ์ด์ฆ์ ๊ณต๊ฐ์ด ํ๋ณด๋๋ค
object
primitive type์ ์ ์ธํ๋ฉด ๋ชจ๋ object
์ค๋ธ์ ํธ: ์ต์ํ ํ ๋๊ฐ์ง ์ด์์ ๋ค์ํ ๋ฐ์ดํฐ๋ฅผ ํ ๊ตฐ๋ฐ์ ๋ฌถ์ด๋์ ๊ฒ
(e.g. ๋ฐฐ์ด, ํจ์ ๋ฑ)
let obj = {
name: 'eillie',
age: 5
};
let obj2 = obj
console.log(obj2.name);
obj.name = 'james';
//obj2์ ๊ฐ๋ ๋ฐ๋๋ค
์ค๋ธ์ ํธ์ ์๋ ํค๋ ๊ฐ๊ฐ ํค๋ง๋ค ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น์ด ๋๋ค
๋ณ์ ํ ๋นํ๋ฉด ์์ ๊ฐ์ด ๋ณต์ฌ๋ผ์ ๋ค์ด์จ๋ค
primitive ํ์
์ ๋ฐ์ดํฐ ์์ฒด๊ฐ ๋ณ์์ ๋ด๊ฒจ์์ด์ ๋ฐ์ดํฐ ์์ฒด๊ฐ ๋ณต์ฌ
์ค๋ธ์ ํธ๋ ์ฃผ์๋ง ๋ณต์ฌ๋ผ์ ๋ค์ด์จ๋ค
primitiveํ์
์ ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ๋ก ํ ๋น๋ ์ ์๋ค
let์ ๊ฐ ๋ณ๊ฒฝ ๊ฐ๋ฅ, const๋ ๊ฐ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ
๊ทผ๋ฐ ์ ์ค๋ธ์ ํธ๋ const๋ก ํ๋๋ฐ ์ ๋ณ๊ฒฝ๋๋?
์ค๋ธ์ ํธ๊ฐ ํ ๋น๋ ๋ ํผ๋ฐ์ค์ ๊ฐ์ ์ ๊ฒจ์๋ค ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ
๋์ obj๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ ์ด๋ฆ์ ๋ฐ๊พธ๋ ๊ฑด ๊ฐ๋ฅ
reference๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ ๊ฒ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
๋ฐ๋ณต์ ์ผ๋ก ์ํ๋๋ ๋ก์ง์ ์ธ ๋
์ฝ๋๋ธ๋ก ์์ ํจ์, ๊ธฐ๋ฅ์ add๋ผ๋ ์ด๋ฆ์ ์ง์ ํด ์ค ๊ฒ - ์ด๊ฑด ์ด์ ํจ์๋ผ๊ณ keword๋ก ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ์๋ฆผ
ํจ์ ์ด๋ฆ๋ ๊ฐ๊ฒฐํ๊ณ ๋ช
ํํ๊ฒ ์ฐ๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ์๋ ํ์
์ด ์๋ค
function add(a,b) {
return a+b;
}
function surprise(operator) {
const result = operator(2,3); //add(2,3)
console.log(result);
}
surprise(add);
๋ฐ์์ค๋ ์ธ์์ ์ด๋ฆ๋ฅผ ํตํด ๋ธ๋ญ ์์์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค
์ด๋ค ๋ฐ์ดํฐ๊ฐ ์ฌ์ง๋ ๋ชจ๋ฅด์ง๋ง ๊ทธ ๋ฐ์ดํฐ์ ์ด๋ฆ์ ํตํด ์ ๊ทผํ๋ ๊ฒ
ํจ์๋ฅผ ๋ง๋๋ ์์ ์๋ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์์ง ์์ ๊ฒ
ํจ์๋ฅผ ํธ์ถํ๋ฉด ์ค์ ๋ก ๋ฐ์ดํฐ๊ฐ ์ ๋ฌ๋๋ค
ํจ์๋ ์ค๋ธ์ ํธ ์ค์ ํ๋ ํจ์๊ฐ ๋ค์ด์๋ ๋ ํผ๋ฐ์ค๊ฐ ๋ฐ์ดํฐ์ ๋ด๊น
ํจ์๋ฅผ ๋ณ์์ ํ ๋นํ๋ฉด ๋ณ์์ ํจ์์ ๋ ํผ๋ฐ์ค๊ฐ ํ ๋น๋๋ ๊ฒ
ํจ์์ ์ด๋ฆ์ ํจ์๊ฐ ์ ์๋ ๊ณณ์ ๊ฐ๋ฆฌํค๊ณ ํจ์์ ์ด๋ฆ์ ๋ค๋ฅธ ๋ณ์์ ํ ๋นํ๋ฉด ํจ์๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ ์ฃผ์๊ฐ ๋ณ์์ ํ ๋น, ๊ฒฐ๊ตญ ๊ฐ์ ํจ์ ๊ฐ๋ฆฌํค๊ณ ์์ด์ ๊ฐ์ ํจ์๋ฅผ ์ํํ๊ฒ ๋๋ค
์๋ฌด๋ฐ ์ธ์๋ฅผ ๋ฐ์ง ์๋ ํจ์๋ ์ธํ์ ๋ฐ์ง ์๋๋ค
์ธ์๋ ์๋ฏธ์๋ ์ด๋ฆ์ฐ๊ธฐ, ์ฐ๋ฆฌ๊ฐ ์ ์ํ ์์ด์
์ธ์๋ฅผ ํจ์๋ก ์ ๋ฌํ๋ฉด, ํจ์๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ ๋ ํผ๋ฐ์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒ๊ณผ ๋์ผํ๋ค
if๊ฐ true๋ฉด ์คํ, false๋ฉด ์คํ ์ ํจ
false: 0, -0, '', null, undefined (๋น์ด์๋ ๊ฐ๋ค)
true: -1, 'false', []
๋ฐฐ์ด์ ์ค๋ธ์ ํธ์ด๊ธฐ์ ํธ๋ฃจ
let num;
num && console.log(num); //false์ด๋ฏ๋ก ์คํ๋์ง ์์
let num2 =9;
num2 && console.log(num2); //์คํ
let obj;
console.log(obj.name); //์๋ฌ, ์๋ ๊ฒ์ ์ ๊ทผํ๋ฉด ํ๋ก๊ทธ๋จ ์์ฒด๊ฐ ์ฃฝ์ด๋ฒ๋ฆฐ๋ค
obj && console.log(obj.name); //obj๊ฐ ์๋ค๋ฉด ์ ๊ทผํ๋ผ๋ ๋ง, ๋ฐ๋ผ์ ์คํ ์ ๋จ
class Counter {
constructor() {
this.counter = 0;
}
increase() {
this.counter++;
}
Counter๋ผ๋ ํด๋์ค์๋ counter๋ผ๋ ๋ณ์๊ฐ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ด ๋ณ์๋ ํด๋์ค ์ด์ฉํด์ ์ค๋ธ์ ํธ ๋ง๋๋ ์๊ฐ 0์ผ๋ก ์ด๊ธฐํ๋๋ค
ํด๋์ค์์ ํจ์ ์ ์ธํ ๋๋ function ์ ์ ์ด๋ ๋จ
ํด๋์ค๋ ๋ค์ํ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ค๊ธฐ ์ํ ์ฒญ์ฌ์ง
๋ ๊ณ ์ฒ๋ผ ์ธ ์ ์๋๋ก ์ฌ์ฌ์ฉ์ฑ ๋์ด๊ธฐ
(ES6, ES11) , ์ธํฐ๋ท ์ต์คํ๋ก๋ฌ์์ ์คํ ์ ๋๋ฏ๋ก ๋ฐ๋ฒจ ์ฌ์ฉํ๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ: ๋ฆฌ์กํธ, ์ต๊ทค๋ฌ, ๋ทฐ
shorthand property names
ํค์ ๋ฐธ๋ฅ์ ์ด๋ฆ์ด ๋์ผํ๋ค๋ฉด ์ถ์ฝํด์ ์์ฑ ๊ฐ๋ฅ
const ellie1 = {
name: 'Ellie',
age: '18',
};
const name = 'Ellie';
const age = '18';
// ๐ฉ
const ellie2 = {
name: name,
age: age,
};
// โจ
const ellie3 = {
name,
age,
};
Destruncturing assignment
์ค๋ธ์ ํธ ํค์ ๋ฐธ๋ฅ์ ์ ๊ทผํ ๋ ํค์ ์ด๋ฆ์ ๊ดํธ ์์ ์ ์, ํค์ ๋ฐธ๋ฅ๊ฐ ๊ฐ๊ฐ ๋ค์๊ณผ ๋ ๋ฒจ์ ๋ง๊ฒ ํ ๋น
const student = {
name: 'Anna',
level: 1,
};
// ๐ฉ
const name = student.name;
const level = student.level;
console.log(name, level);
// โจ
const { name, level } = student;
console.log(name, level);
const { name: studentName, level: studentLevel } = student;
console.log(studentName, studentLevel);
// array
const animals = ['๐ถ', '๐ฝ'];
// ๐ฉ
const first = animals[0];
const second = animals[1];
console.log(first, second);
// โจ
const [first, second] = animals;
console.log(first, second);
Spread syntax
์ฃผ์์ ์ฐธ์กฐ๊ฐ์ ๋ณต์ฌํ๋ ๊ฒ์ด๊ธฐ์ ๊ฐ ๋ฐ๊พธ๋ฉด ์๋์ ์ ์ฉ๋จ
const obj1 = { key: 'key1' };
const obj2 = { key: 'key2' };
const array = [obj1, obj2];
// array copy
const arrayCopy = [...array];
console.log(array, arrayCopy);
const arrayCopy2 = [...array, { key: 'key3' }];
obj1.key = 'newKey';
console.log(array, arrayCopy, arrayCopy2);
// object copy
const obj3 = { ...obj1 };
console.log(obj3);
// array concatenation
const fruits1 = ['๐', '๐'];
const fruits2 = ['๐', '๐ฅ'];
const fruits = [...fruits1, ...fruits2];
console.log(fruits);
// object merge //๋์ผํ key ๋ฉด ๊ฐ ๋ฎ์ด์์ฐ๋ฏ๋ก ์ฃผ์
const dog1 = { dog: '๐' };
const dog2 = { dog: '๐ถ' };
const dog = { ...dog1, ...dog2 };
console.log(dog);
console.clear();
default parameters
// ๐ฉ
function printMessage(message) {
if (message == null) {
message = 'default message';
}
console.log(message);
}
printMessage('hello');
printMessage();
// โจ
function printMessage(message = 'default message') {
console.log(message);
}
printMessage('hello');
printMessage();
ternary operator
// ๐ฉ
let component;
if (isCat) {
component = '๐ธ';
} else {
component = '๐ถ';
}
console.log(component);
// โจ
const component = isCat ? '๐ธ' : '๐ถ';
console.log(component);
console.log(isCat ? '๐ธ' : '๐ถ');
template literals
const weather = '๐ค';
const temparature = '16ยฐC';
// ๐ฉ
console.log(
'Today weather is ' + weather + ' and temparature is ' + temparature + '.'
);
// โจ
console.log(`Today weather is ${weather} and temparature is ${temparature}.`);
optional chaining
const person1 = {
name: 'Ellie',
job: {
title: 'S/W Engineer',
manager: {
name: 'Bob',
},
},
};
const person2 = {
name: 'Bob',
};
// ๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ
function printManager(person) {
console.log(person.job.manager.name);
}
printManager(person1);
// printManager(person2);
// ๐ฉ๐ฉ๐ฉ
function printManager(person) {
console.log(
person.job
? person.job.manager
? person.job.manager.name
: undefined
: undefined
);
}
printManager(person1);
printManager(person2);
// ๐ฉ
function printManager(person) {
console.log(person.job && person.job.manager && person.job.manager.name);
}
printManager(person1);
printManager(person2);
// โจ
function printManager(person) {
console.log(person.job?.manager?.name);
}
printManager(person1);
printManager(person2);
nullish coalescing operator
const name = 'Ellie';
const userName = name || 'Guest';
console.log(userName);
const name = null;
const userName = name || 'Guest';
console.log(userName);
// ๐ฉ
const name = '';
const userName = name || 'Guest';
console.log(userName);
const num = 0;
const message = num || 'undefined';
console.log(message);
// โจ
const name = '';
const userName = name ?? 'Guest';
console.log(userName);
const num = 0;
const message = num ?? 'undefined';
console.log(message);
๊ฐ๋ฐ์ ๊ณต๋ถ๋ฐฉ๋ฒ๊ณผ ๊ฒฝ๋ ฅ๊ด๋ฆฌ๋ฒ
๋๊ฒ ๊ณต๋ถํ๊ณ ์ ๋ฌธ์ฑ ํ๋ ๊ด๋ฆฌ + ํ ์ ์์
ํ๋ก ํธ์๋
-- ๊น์ด
1. ๊ธฐ๋ณธ: html, css, javascript
2. ํ๋ ์์ํฌ: ํ์
์คํฌ๋ฆฝํธ, ๋ทฐ, ๋ฆฌ์กํธ
3. ์ฌ์ฉ์์๊ฒ ๋ฒ๋ค๋ง๋๋ ์ฌ์ด์ฆ ์ค์ด๊ธฐ ๊ณ ๋ ค
4. ๋ ์ด์ง๋ก๋ฉ, ๋ฒ๋ค๋ง ์คํ๋ฆฌํ
ํด์ ์ ๋ฌํ๊ธฐ, ํผํฌ๋จผ์ค ๋ถ์, ์ ๋๋ฉ์ด์
๋ ์ดํธ ์ค์ด๊ณ ์ตํฐ๋ง์ด์ ์ด์
ํ๊ธฐ
5. ๋์์ธ์คํฌ, ๋ฐฑ์๋ ํต์ ๋ฐฉ๋ฒ, ๋ฐฑ์๋ ๋ฐ์ดํฐ ๋ฐ์์ค๊ณ ๋ ์ต์ ํ๋ ์๋น์ค ๋ ์ด์ด ๋ง๋๋ ๋ฒ
-- ๋์ด
-- ํ ์
์ปดํจํฐ ๊ณตํ์ ๊ธฐ๋ณธ ์ง์(์ด์์ฒด์ , ๋คํธ์ํฌ, ์ด์
๋ธ๋ฆฌ, ์๋ฒ ๋๋, ์ํ(ํ๋ ฌ,ํต๊ณ)
์๋ฃ๊ตฌ์กฐ(๋ฐฐ์ด,๋ฆฌ์คํธ,๋งต ์ธ์ ์ด๋ป๊ฒ ์จ์ผ ํจ์จ์ ์ผ๋ก ๋ฐ์ดํฐ๊ด๋ฆฌ ํ ์ ์๋์ง ์ฐจ์ด์ )
์๊ณ ๋ฆฌ์ฆ(์ํฉ๊ณผ ์ฃผ์ด์ง ์๋ฃ๊ตฌ์กฐ์ ๋ฐ๋ผ ์ด๋ค ์๊ณ ๋ฆฌ์ฆ ์จ์ผ ์๊ฐ๊ณผ ๊ณต๊ฐ์ ํจ์จ์ ์ผ๋ก ์ฐ๋์ง)
๊นจ๋ํ ์ฝ๋, test driven code, refactoring, code review
๊ณต๋ถ๋ฒ
๋น์ ๊ณต์๋ ์ธ์ด ํ๋ ๋ง์คํฐํ๊ณ ์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ ๊ณต๋ถ
-> ์๋ฐ์คํฌ๋ฆฝํธ ์ผ๋จ ๋ง์คํฐํ๊ธฐ
call stack = mechanism the JS interpreter uses to keep track of its place in a script that calls multiple functions
how js knows what function is currently being run and what function are called from within that function, etc like finger in a book
stack: basic data structure in computer science
how it works
When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function.
Any functions that are called by that function are added to the call stack further up, and run where their calls are reached.
When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing.
์ฝ์คํ ๋ณด์ฌ์ฃผ๋ ์ฌ์ดํธ
ํน์ ํฌ๋กฌ ๊ฐ๋ฐ ํด์ source๋ฅผ ๋ด๋ ์ ์ ์๋ค
๊ธฐํ์ ์์ฑ, ๊ตฌํํ ๊ธฐ๋ฅ๊ณผ ํ๋ฉด ๊ธฐํํจ