본문 바로가기
CodeStates/└ JavaScript(Pre)

객체

by Dream_World 2020. 6. 18.

객체 (Object)

  • 주소록에 주로 사용

let user = { 

 firstName : 'Lee',

 lastName : 'Sangho',

 email : 'jesussangho@gmail.com',

 city : 'Seoul',

 phone : '010-0000-0000'

};
 
  • firstName : 키(key)

  • 'Lee' : 값(value)

  • 객체는 키와 값 쌍(key-value pair)으로 이루어져 있음

Dot notaion (object.key)

let user = { 

 firstName : 'Lee',

 lastName : 'Sangho',

 email : 'jesussangho@gmail.com',

 city : 'Seoul',

 phone : '010-0000-0000'

};

/*=============================*/

user.firstName; // 'Lee'
"Seoul"
user.city; // 'Seoul'
"Lee"

Bracket notation (object['key'])

let user = { 

 firstName : 'Lee',

 lastName : 'Sangho',

 email : 'jesussangho@gmail.com',

 city : 'Seoul',

 phone : '010-0000-0000'

};

/*=============================*/

user['firstName']; // 'Lee'
"Lee"
user['city']; // 'Seoul'
"Seoul"
 

객체 추가 (object.['key'] = 'value')

let user = { 

 firstName : 'Lee',

 lastName : 'Sangho',

 email : 'jesussangho@gmail.com',

 city : 'Seoul',

 phone : '010-0000-0000'

};

/*====================================*/

user['add'] = '주소';
"주소"
user.tags = [ '#javascript', '#blog' ];
["#javascript", "#blog"]

/*====================================*/

add: "주소"
tags: ["#javascript", "#blog"]
 
  • Dot/Bracket notation을 이용해 값을 추가

객체 삭제 (delete object.key)

let user = { 

 firstName : 'Lee',

 LastName : 'Sangho',

 email : 'jesussangho@gmail.com',

 city : 'Seoul',

 phone : '010-0000-0000'

};

/*====================================*/

delete user.phone;
true
delete user.email;
true
/*====================================*/

firstName: "Lee"
LastName: "Sangho"
add: "주소"
city: "Seoul"
 

객체 조회 ('key' in object)

let user = { 

 firstName : 'Lee',

 LastName : 'Sangho',

 email : 'jesussangho@gmail.com',

 city : 'Seoul',

 phone : '010-0000-0000'

};

/*====================================*/

'city' in user;
true
'source' in user;
false

 

 

'CodeStates > └ JavaScript(Pre)' 카테고리의 다른 글

map  (0) 2020.06.20
for Each  (0) 2020.06.20
push, pop, shift, unshift, slice  (0) 2020.06.18
타입 구분  (0) 2020.06.18
문자열 다루기  (0) 2020.06.18

댓글