즉시 실행 함수
(function () {
console.log('즉시 실행 함수 실행!');
})();
코드 내에서 맨 처음 실행 시 단 한번만 실행할 때 사용되는 방법
속성과 메소드
const obj = {
// 속성(property)
id: 10,
age: 20,
// 메소드(method)
getAgeGetter: function() {
return 20;
},
getAgeGetter2: () => {
return 20;
},
getAgeGetter3() {
return 20;
}
}
getter, setter
class Person {
private _bloodType: string;
constructor(bloodType: string) {
this._bloodType = bloodType;
}
// getter
get bloodType() {
return `${this._bloodType} 형`;
}
// setter
set bloodType(btype: string) {
if (btype === 'A' || btype === 'B' || btype === 'O' || btype === 'AB') {
this._bloodType = btype;
}
}
}
const p1 = new Person('B');
console.log(p1.bloodType); // getter
p1.bloodType = 'C'; // setter
console.log(p1.bloodType); // getter
Getter 와 Setter 는 일반 객체에서는 만들 수가 없으니까
클래스로 만들어진 인스턴스 객체에서 만들 수 있는 스팩 중에 하나입니다.
JavaScript 로 객체 생성 및 구성 방법
const myObj = Object.create(null, {
name: {
value: 'Kim heetae',
writable: true, // true 면 name 속성에 새로운 값 할당 가능
configurable: true, // true 면 name 속성 삭제 가능 / false면 불가능
}
});
myObj.name = 'Heetae';
delete myObj.name;
(1) 반환 값으로 전달되는 함수 호출 식 → return 값으로 함수의 값을 반환 (함수 식 반환)
(2) 반환 값으로 전달되는 함수 선언 문 → return 값으로 함수를 반환 (함수 문 반환)
*→ 더 나아가 표현력의 차이를 만듦*
반환 값으로 전달되는 함수 호출 식
function ul(tag: string): string {
return `<ul>${tag}</ul>`;
}
function ol(tag: string): string {
return `<ol>${tag}</ol>`;
}
function makeLI(container: (tag: string) => string, lists: string[]): string {
let li = [];
for (const list of lists) {
li.push(`<li>${list}</li>`);
}
return container(li.join('')); // 함수 호출 식 반환
}
console.log(makeLI(ul, ['월', '화', '수', '목', '금', '토', '일']));
console.log(makeLI(ol, ['봄', '여름', '가을', '겨울']));
반환 값으로 전달되는 함수 선언 문
function discountPrice(discountRate: number) {
return function (price: number): number { // 함수 선언 문 반환
return price - (price * (discountRate * 0.01));
}
}
// 표현력1 (비추천)
console.log(discountPrice(30)(567000));
console.log(discountPrice(10)(567000));
// 표현력2 (추천)
let summerPrice = discountPrice(30);
let winterPrice = discountPrice(10);
console.log(summerPrice(567000));
console.log(winterPrice(567000));
대량의 데이터를 불러오는데 시간이 걸리거나, setTimeout()으로 시간을 지연 시킬 때 비동기적 상황이라 할 수 있다.
(setTimeout, 시간이 걸리는 데이터 처리가 없으면 비동기 코드가 아니고 동기적인 코드이다.)
이런 비동기 코드를 동기 코드와 유연하게 처리하기 위해서는 Promise / async 를 사용한다.