JavaScript Object{객체}

wanni kim·2021년 4월 17일
0
post-thumbnail

객체

Object = {Property key: Property value}

객체의 프로퍼티 추가, 조회, 변경, 삭제
조회방법 : dot notation, bracket notation

객체 property value 할당
object.a = "hello"
object['a'] = "hello"
객체 객체 삭제 : delete
객체 반복문 for ...in

let user = {
  Name: 'changwan',
  job: 'warrior',
  level: '200',
  rank: 'top100'
}
user.Name; // 'changwan' (dot notaion)
user['job']; // 'warrior' (bracket notation)

user[job]; // job is undefined value 레퍼런스 에러
let work = 'job';
user[work]; // 'warrior' 

bracket notaion은 동적인 변수가 들어올때 사용해야한다. dot notaion은 사용할 수 없다.
아래 코드 참조

let car = {
  model: 'colvet',
  series: 'c8',
  year: '2020'
}
function getProperty1(obj, proName) {
  return obj[proName];
}
function getProperty2(obj, proName) {
  return obj.proName;
}

let obj1 = getProperty(car,'model')
console.log(obj1); // 'colvet'
let obj2 = getProperty(car,'series')
console.log(obj2); // undefined

객체 키:값 추가/삭제 예제

let insta = {
  writer: 'wan',
  date: '20210401',
  content: 'gloryDay'
};
// 추가하는법
insta['category'] = 'wishMyself';
insta.isPublic = true;
insta.tags = ['#자랑','#마음만은 일류']; // 배열도 추가 가능!
// 삭제하는법
delete insta.content; // content 키 값에 담긴 value까지 삭제.

// in 연산자를 이용 해당 키가 있는지 확인.
'writer' in insta; // true
'likes' in instal // false
profile
Move for myself not for the others

0개의 댓글