Object

const student = {
	name: ‘angel’,
	id: 1004
}

console.log(student)


// Object {
//	id: 1004,
//	name: “angel”
// } 출력

              

Object 접근

student.name //”angel”

student[‘id’] //1004

 

Object 추가

student.major = ‘computer science’;

student[‘semester’] = 8

 

Object 삭제

delete student.semester;

 

 

Object – 단축 프로퍼티

const name = ‘angel’;
const id = 1004;
const student = {
              name: name,
              id: id,
              major: ‘computer science’
}
const student = {
              name,
              id,
              major: ‘computer science’
}

 

 

Object – 프로퍼티 존재 여부 확인

student.birthDay; //undefined

‘birthDay’ in student; //false

‘name’ in student; //true

    

 

for … in 반복문

for(let key in student) {
	console.log(key); //“name” “id” “major” 출력
	console.log(student[key]); //“angel” 1004 “computer science” 출력
}

 →  객체를 순회하면서 값을 얻을 수 있다.

 

 

함수를 통한 객체 생성

function makeObject(name, id) {
	return {
		name,
		id,
		major: “computer science”
	};
}

const student = makeObject(“angel”, 1004);
console.log(student);


//	Object {
//		id: 1004,
//		major: “computer science”
//		name: “angel”
//	} 출력

 

 

 

↓[코딩앙마] 자바스크립트 기초 강좌 링크

https://www.youtube.com/watch?v=DuXA1t6hl3U 

 

 

+ Recent posts