1️⃣ 데이터 타입을 자기가 맞다고 생각하는대로 변환하므로 정확한 데이터 타입을 넘겨줘야한다
2️⃣ 함수의 입력과 출력이 선택적이다
일반적인 함수는 입력과 출력이 필수이다
3️⃣ 함수에 부가 기능이 있어도 된다 ex) console.log()
- 함수는 변수에 담을 수 있음 <화살표 함수>
const add = (x,y) => x+y; //익명 함수 ver. const add = function(x,y) {return x+y};
- 함수의 인자로 넘길 수 있음
function exec(fn) { fn(1); } exec(console.log); exec(alert);
- 함수의 반환값이 될 수 있음
function curry(f) { return function(a) { return function(b) { return f(a,b); }; }; } function add(a,b) { return a+b; } let curriedAdd = curry(add); console.log(curriedAdd(1)(2));
🔁동기 : 직렬적으로, 순서대로
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then(res => res.json())
.then(console.log);
console.log('test');
🔀비동기 : 병렬적으로, 먼저 끝나는대로
→프로미스
함수가 반환하는 값에 Promise
가 들어있으면 비동기로 처리해야함
1. Pending
프로미스 처리중
2. Fulfilled
프로미스 이행 (정상 처리 완료)
.then(res ⇒ res.json())
//.then(이전 데이터 ⇒ json으로 변환해서 리턴하는 함수)
Rejected
프로미스 실패 (비정상적으로 처리 완료)배열의 값을 별도의 변수로 선언할 때 사용
const me={
name: "임채리",
age: 23,
}
const {name} = me;
//const name = me.name;
const {name, age} = me;
//두개의 변수 동시에 선언
console.log(name);
const a = arr[0];
const b = arr[1];
const [a] = arr;
const [a,b] = arr;
객체에는 객체만 배열에는 배열만 가능
const me ={
name: "임채리",
age: 23,
}
const militaryMe = {
위에서 내용 가져오기
...me,
militaryState: false,
}
console.log(militaryMe);
스프레드의 반대 기능
const me ={
name: "임채리",
age: 23,
militaryState: false,
}
const { militaryState, ...another } = me;
console.log(another);
const numbers = [0,1,2,3,4,5,6];
const [zero, ...rest] = numbers;
console.log(rest);