-
[Javascript] 객체리터럴 vs 생성자함수, 그리고 thisTo infinity/Javascript 이론 2021. 6. 27. 14:31
객체 리터럴 vs 생성자 함수
// 객체리터럴 = { key : value } 즉, : 로 연결 const profile = { name: 'buzz', born: 1995 } console.log(profile); //{ name: 'buzz', born: 1995 } // 생성자함수 = { this.key = value 인자} function inform(name, born) { this.name = name, this.born = born } const profile2 = new inform('buzz',1995) console.log(profile2); //inform { name: 'buzz', born: 1995 }
1. 객체리터럴로 객체를 생성할 때랑 생성자함수로 생성할 때랑 키랑 값을 연결하는 연결자가 다르다.
객체리터럴 = { key : value } 즉, : 로 연결
생성자함수 = { this.key = value 인자} 즉, =로 연결
2. 또한, 생성자함수로 새로운 객체를 생성할 때에는 꼭!! new를 적어줘야 한다.
!! new 생성자함수( 인수 ) 니까, new 뒤에는 생성자함수가 들어가야한다.
new result() 라고 친다고 result = {} 라는 객체가 생성되는게 아니다.
3. 그리고 생성자함수에서 this.프로퍼티 키 이름 = 프로퍼티 값 이다.
일반적으로 프로퍼티 키 이름과 값을 동일하게 적는데 다르게 적어도 작동하는데 문제는 없다.
나는 해당 이름과 값이 동일해서 좀 헷갈렸었다.
this
this는 생성자함수 내부에서, 그리고 객체의 메서드 내부에서 의미가 있다.
즉, 객체를 생성하는 생성자함수
그리고 객체생성 후 그 안에서 작동하는 메서드에서 사용한다고 보면 된다.
'To infinity > Javascript 이론' 카테고리의 다른 글
[Javascript 이론] Map (feat. object) (0) 2021.07.04 [Javascript 이론] 일반함수, ES6 메서드 함수, 화살표함수 비교 (0) 2021.07.04 [Javascript] this (0) 2021.06.27 [Javascript 200] Symbol (0) 2021.06.27 [Javascript 200] 배열과 객체 (ES6기능) (0) 2021.06.26