[javascript] this와 new

Chaedie·2022년 6월 8일
0

Javascript

목록 보기
3/8
post-thumbnail

this, new

function song(title, writer, singer, released) {
  this.title = title;
  this.writer = writer;
  this.singer = singer;
  this.released = released;
  console.log(this);
}


// 인스턴스화 ㄴㄴㄴ
const song1 = song();
const song2 = new song();
  • new song() 으로 인스턴스화 하지 않으면 아래와 같이 this가 song을 가르키는게 아니고, window를 가르킨다.
  • 반대로 인스턴스화를 해주면 this는 song을 가르킨다.

출처

데브리 유튜브 (<- 정말 좋음)

profile
TIL Blog - Today's Intensive Learning!

0개의 댓글