728x90
반응형
생성자 함수 = 붕어빵 틀!
함수의 이름 첫글자는 대문자로 쓴다.
function User (name, age) {
// this = {}
this.name = name;
this.age = age;
// return this;
}
사실은 주석처리 된 부분이 숨어있는 것.
생성자 함수는 new 연산자로 호출한다.
const user1 = new User('mika', 20);
const user2 = new User("misa", 22);
const user3 = new User("mina", 21);
new로 호출하지 않으면 리턴되는 값이 없어서 undefined반환
function Item (title, price) {
this.title = title;
this.price = price;
this.showPrice = () => {
console.log(`가격은 ${price}`);
};
}
const item1 = new Item("인형", 2000);
const item2 = new Item("가방", 8200);
const item3 = new Item("지갑", 9000);
item2.showPrice();
// 생성자함수 안의 메소드 호출
728x90
반응형
LIST
'코딩 메모장' 카테고리의 다른 글
StyledComponents로 리액트 스타일도 컴포넌트로 만들기 (0) | 2023.03.16 |
---|---|
[REACT 오류 해결] gh-page 배포 경로 오류 (4) | 2021.08.20 |
[REACT 메모] REACT Hook - useInput(1) (2) | 2021.08.15 |
[REACT 메모] .env로 githib에서 키 관리 (0) | 2021.08.14 |
[JAVASCRIPT 메모 ] 오브젝트 메소드 - Object.assign(); / /Object.keys(); / Object.values(); /Object.entries(); / Object.fromEntries(); (0) | 2021.08.13 |
댓글