this에 대하여

Dorogono·2022년 6월 15일
0

JS 알아보기

목록 보기
17/19
post-thumbnail

this란.

자신이 속한 객체 혹은 자신이 생성할 인스턴스를 가리키는 자기 참조 변수다.

function Circle(radius) {
  // 생성자 함수가 없으므로 자신이 생성할 인스턴스를 가리키는 식별자는 알 수 없다.
  ????.radius = radius;
}

Circle.prototype.getDiameter = function() {
  // 여기도 마찬가지로 생성자 함수가 없으므로 식별자를 알 수 없다.
	return 2 * ????.radius;
}

// 생성자 함수로 인스턴스를 생성하면서 가리키는 식별자가 생성됐다.
const circle = new Circle(5)

this가 가라키는 것

console.log(this); // window

function example() {
	console.log(this); // window
}

const person = {
  name: 'Lee',
  getName() {
    console.log(this); // {name: 'Lee', getName: f}
  }
}

// 생성자 함수가 있어야 식별자 생성됨.
function Person(name) {
  this.name = name;
  console.log(this); // Person {name: 'Lee'}
}
const me = new Person('Lee');

strict모드에서 this

여기에서는 undefined로 바인딩된다.
일반 함수 내부에서는 일반적으로 사용하지 않기 때문이다.
thisClass에서 주로 사용되기 때문이다.

profile
3D를 좋아하는 FE 개발자입니다.

0개의 댓글