๐ ์ดํํ ์ฝ๋ฉ ์๊ฐ 3hour
๐๐ผ ๋ฉํ ํ๋ณตํ๋ ค๊ณ ๋
ธ๋ ฅํจ
๐๐ผ ๋ฉํ ๊ด๋ฆฌ ์คํจ
์๋ฐ์คํฌ๋ฆฝํธ๋ ํด๋์ค๊ฐ ์ถ๊ฐ๋์์ง๋ง ์๋ฐ์ฒ๋ผ ์์ํ objcet oriented laguage๊ฐ ์๋
prototype์ ๋ฒ ์ด์ค๋ก ํ ๊ฐ์ง์ object oriented language์ด๋ค
๋ฐ๋ผ์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ ์ฐจ์ ์ธ์ด(procedural language) ์ค ํ๋์ด๋ค
function์ sub-program์ด๋ผ๊ณ ๋ถ๋ฅด๊ธฐ๋ ํ๋ค
function์ input(parameter)์ ๋ฐ์์ output์ผ๋ก returnํ๋ค
function name(param1, param2) { body...return; }
one function === one thing
naming: doSomething, command, verb
e.g. createCardAndPoint -> createCard, createPoint
function is object in JS
์๋ฐ์คํฌ๋ฆฝํธ ํจ์์์๋ ํ์ ์ด ๋ช ํํ์ง ์๋ค
-> ์ซ์๋ฅผ ์จ๋ ๋ฌธ์๋ก ๋ณํ๋ผ์ ์ถ๋ ฅ
-> typescript๋ฅผ ์ฐ๋ฉด ์ด๋ฐ ๋ฌธ์ ํด๊ฒฐ
// in javascript
"use strict";
function log(message) {
console.log(message);
return 0;
}
// in typescript - ํ์
์ ์ฝ๊ฒ ํ๊ณ api ํํ๋ก ์ ๊ณตํ ๋ ์ ์ฉ
function log(message: string): number {
console.log(message);
return 0;
}
primitive parameter: passed by value
ํจ์์ ์ ๋ฌ๋๋ ๋งค๊ฐ์ธ์ ์ค primitive type์ ๊ฐ์ด ๋ฉ๋ชจ๋ฆฌ์ ๊ทธ๋๋ก ์ ์ฅ๋์ด ์๊ธฐ์ ๊ฐ์ด ์ ๋ฌ๋จ
object parameters : passed by reference
objective type์ ๋ฉ๋ชจ๋ฆฌ์ reference๊ฐ ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ reference๊ฐ ์ ๋ฌ๋๋ค
default parmaters (added in ES6)
function showMessage(message, from='unknown') {
console.log(`${message}, ${from}`);
}
rest parameters (added in ES6)
... -> rest parameters, ๋ฐฐ์ด ํํ๋ก ์ ๋ฌ๋จ
function printAll(...args) {
for (let i = 0; i <args.length; i++) {
console.log(args[i]);
}
for (const arg of args) {
console.log(arg);
} //์ด๋ฐ ์์ผ๋ก ๊ฐ๋จํ๊ฒ ์ถ๋ ฅํ ์ ์๋ค
args.forEach(arg) => console.log(arg));
}
printAll('dream','coding','ellie');
function์ object์ ์ผ์ข
์ด๋ค
ํจ์ ์ ์ธ ํ ํจ์๋ช
. -> ํจ์๊ฐ ์ค๋ธ์ ํธ๋ก ์ ํ๋๊ธฐ ๋๋ฌธ์ ํจ์(์ค๋ธ์ ํธ)์ ์์ฑ๊ฐ์ ํ์ธํ ์ ์๋ค
block scope, global scope
closer, lexical environment์ ๋ค์์ ๋ฑ ํ๋์ ๊ฐ๋ ์์ ํ์๋ ๊ฒ์
"๋ฐ์์๋ ์์ด ๋ณด์ด์ง ์๊ณ ์์์๋ง ๋ฐ์ ๋ณผ ์ ์๋ค" (ํดํธ๋ ์ ๋ฆฌ์ฐฝ)
๋ธ๋ญ ์์์ ๋ณ์ ์ ์ธํ๋ฉด ์ง์ญ ๋ณ์ -> ์์์๋ง ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค
๋ธ๋ญ ๋ฐ์์๋ ๋ธ๋ญ ์์ ์ ๊ทผ ๋ชปํ์ง๋ง ๋ธ๋ญ ์์์๋ ๋ธ๋ญ ๋ฐ์ ๋ณ์์ ์ ๊ทผ ๊ฐ๋ฅํ๋ค
return ํ์ ์ด ์๋ ํจ์๋ return undefined;๊ฐ ๋ค์ด๊ฐ ์๋ ๊ฒ๊ณผ ๊ฐ๋ค
early return, early exit
๋ธ๋ญ์ผ๋ก ๋ก์ง์ ์์ฑํ๋ฉด ๊ฐ๋
์ฑ์ด ๋ง์ด ๋จ์ด์ง๋ค (e.g. if, else)
๋ฐ๋ผ์ ์กฐ๊ฑด์ด ๋ง์ง ์์ ๋๋ return์ผ๋ก ๋นจ๋ฆฌ ํจ์๋ฅผ ์ข
๋ฃํ๊ณ
์กฐ๊ฑด์ด ๋ง์ ๋๋ง ํ์ํ ๋ก์ง๋ค์ ์ญ ์คํํ ์ ์๋๋ก ์ฝ๋๋ฅผ ์งค ๊ฒ
//bad
function upgradeUser(user) {
if (user.point > 10) {
//long upgrade logic
}
}
//good
function upgradeUser(user) {
if (user.point <= 10) {
return;
}
//long upgrade logic
}
first-class function
function are treated like any other variable
can be assigned as a value to variable
can be passed as an argument to other functions
can be returned by another function
function expression
a function declaration can be called earlier than it tis defined. (hoisted)
a function expression is created when the execution reaches it.
anonymous function vs named function
const print = function () {
console.log('print');
};
const print = function print() {
console.log('print');
};
print();
const printAgain = print;
printAgain();
const sumAgain = sum;
console.log(sumAgain(1,3));
ํจ์๋ฅผ ์ ์ธํจ๊ณผ ๋์์ ๋ฐ๋ก ๋ณ์์ ํ ๋นํ ์ ์๋ค
named function is better debugging in debugger's stack traces
recursion : ํจ์ ์์์ ํจ์ ์์ ์ค์ค๋ก๋ฅผ ๋ถ๋ฅด๋ ๊ฒ -> ํ๋ก๊ทธ๋จ์ด ์ฃฝ์ (ํผ๋ณด๋์น๋ ๋ฐ๋ณต๋๋ ํ๊ท ๊ฐ ๊ณ์ฐํ ๋๋ง) -> ๊ณ์ ํ๋ฉด call stack์ด ๊ฝ ์ฐจ๊ฒ ๋๋ค
callback function using function expression
function randomeQuiz(answer, printYes, printNo) {
if (answer === 'love you') {
printYes();
} else {
printNo();
}
}
always anonymous
const simplePrint = () => console.log('simplePrint');
const add = (a,b) => a+ b;
// ๋ธ๋ญ ๋ฃ์ด์ ์ฒ๋ฆฌํ ์๋ ์๋ค (return ์จ์ผ ํจ)
const add = (a,b) => {
return a+ b;
};
immediately invoked function expression
//ํจ์ ํธ์ถ ๊ธฐ์กด ๋ฐฉ์
function hello() {
console.log('IIFE');
}
hello();
//IIFE ๋ฐฉ์ - ์ ์ธ๊ณผ ๋์์ ํธ์ถํ ์ ์๋ค
(function hello() {
console.log('IIFE');
})();
function calculate(command, a, b)
command: add, substract, divide, multiply, remainder
calculate(add,2,3)
class vs object
object-oriendted
class person{
name; //property - ์์ฑ(field)
speak(); //function - ํ๋(method)
}
property
์ด๋ค ๊ฐ์ด ๋ค๋ฅธ ๊ฐ๊ณผ ์ฐ๊ด์ ๊ฐ์ง๊ณ ์์ ๋
๋ฌธ์์ด.length ์์ length๋ฅผ ํ๋กํผํฐ๋ผ ํ๋ค -