
js를 공부하다보면 마주치는 녀석이 있다.
오픈 소스를 보면 진짜 주구장창 나오는 녀석인데, 진짜 파악이 1도 안되었다.
아무것도 모르는 까막눈인 상태로 코드를 읽어보면 얘를 무시할 수 없는 녀석이라고 판단.
조금씩 알아가보자...... 두둥......
It also has some differences between strict mode and non-strict mode.
엄격모드와 비엄격 모드로 구분한다 했고,
In most cases, the value of this is determined by how a function is called (runtime binding).
It can't be set by assignment during execution, and it may be different each time the function is called.
The bind() method can set the value of a function's this regardless of how it's called, a
nd arrow functions don't provide their own this binding
(it retains the this value of the enclosing lexical context).
대부분 this의 값은 호출한 방법에 의해 결정된다고 한다.
그리고 this가 무식하게 하나로 딱 정해져 있는게 아닌,
내가 어디에서 어떻게 호출하느냐에 따라 값이 달라진다 한다.~코드를 짜보면서 파악해봐야겠다~
let person = {
name: '홍길동',
age: 30,
married: true,
introduce: function () {
return `저는 ${this.name}, ${this.age}살이고 `
+ `${this.married ? '기혼' : '미혼'}입니다.`;
}
}
console.log(person.introduce());
여기서 this는 객체 person을 의미함.
즉 객체 안에 들어가 있는 함수는 객체의 다른 프로퍼티에 접근할때 this를 사용한다.
근데 여기서는 ❌화살표 함수로는 쓰면 안댐❌ 가리키는 this가 다르다.