상추의 IT저장소
JS) 객체(object) 본문
객체
자바스크립트에서 원시타입을 제외한 모든 데이터 타입(객체, 함수, 배열, 정규 표현식 등)은 객체다.
객체는 여러가지 값을 가질 수 있으며, 함수도 가질 수 있다.
객체가 보유한 값을 프로퍼티라고 하며, 객체가 보유한 함수를 '메서드'라고 한다.
객체 생성 방식
객체를 생성하는 방법은 객체 리터럴을 사용하는 방식,
1)객체 리터럴
변수처럼 객체를 생성하는 방식
let myObj = {
name: 'lettuce',
age: 20,
hello: function(){
return `이름은 ${this.name}이고, 나이는 ${this.age}입니다.`;
}
};
console.log(myObj);
>>> 결과값

2) 생성자 방식
new Constructor() 방식으로 객체를 생성하는 방식이다.
var myObj = new Object();
객체에 프로퍼티 추가 방법
객체에 추가 하는 방법은 dot notation 표기법을 이용하는 방법과 bracket notation 표기법 등이 있다.
// 1. dot notation 표기법
obj.name = 'lettuce';
obj.age = 20
obj.address = 'Inchoen'
console.log(obj);
// 2. Bracket Notation 표기법
let lettuce2 = {};
lettuce2['name'] = 'lettuce';
lettuce2['age'] = 20;
lettuce2['address'] = 'inchoen';
console.log(lettuce2);
>>> 출력값

객체에 프로퍼티 삭제 방법
let lettuce2 = {};
lettuce2['name'] = 'lettuce2';
lettuce2['age'] = 30;
lettuce2['address'] = 'inchoen';
console.log(lettuce2);
delete lettuce2.address;
console.log(lettuce2);
>>>출력값

'Javascript' 카테고리의 다른 글
| JS) Array.map() (0) | 2022.11.15 |
|---|---|
| JS) 전역객체와 래퍼객체 (0) | 2022.11.15 |
| JS) 타입변환 (0) | 2022.11.11 |
| JS) 배열 생성, 추가, 삭제 (0) | 2022.11.10 |
| JS) NPM 소개 및 사용법 (0) | 2022.11.06 |