객체를 생성하는 방법에는 두 가지가 있다.
- 객체 리터럴
- 생성자 함수
단 하나의 객체만을 생성할 때는 직관적이고 간편한 객체 리터럴을 사용하고,
같은 객체를 대량생산할 때는 생성자함수를 사용한다.
객체 리터럴
const goodCoder = {
name : 'Noran',
age : 20,
}
* 수정/유지보수가 용이하도록 마지막 값에는 되도록 ,를 붙여주자.
객체.접근
goodCoder.name
goodCoder['age']
객체.추가
goodCoder.gender = "Female";
goodCoder.clothes = "sweatshirt";
객체.삭제
delete goodCoder.clothes;
생성자 함수 (object constructor function)
function Coder(name, age, coding) {
this.name = name;
this.age = age;
this.coding = coding;
}
*생성자 함수의 첫 글자는 생성자임을 나타내기 위해 대문자로 작성한다. (파스칼 표기법)
프로토타입 생성
let coder1 = new Coder("Noran",10,"html");
let coder2 = new Coder("Paran",20,"css");
let coder3 = new Coder("Chorok",30,"js");
//coder1
Coder {name: "Noran", age : 10, coding : "html"}
//coder2
Coder {name: "Paran", age : 20, coding : "css"}
//coder3
Coder {name: "Chorok", age : 30, coding : "js"}
new 연산자를 사용해서 객체 프로토타입을 생성할 수 있다.
출처 : 코딩앙마 https://www.youtube.com/@codingangma
'🟨 JavaScript > 개념' 카테고리의 다른 글
| [mdn] 기본값 매개변수 (0) | 2022.04.15 |
|---|---|
| [생활코딩, 드림코딩, 코딩앙마] 연산자(Operator)와 break, continue (0) | 2022.04.14 |
| [코딩앙마 외] 객체(Object)와 프로퍼티(property) (0) | 2022.04.14 |
| [코딩앙마] 함수선언문과 함수표현식, 그리고 화살표함수 (추가) (0) | 2022.04.14 |
| [드림코딩/코딩앙마] scope와 hoisting, 그리고 TDZ (추가) (0) | 2022.04.14 |