=> 무조건 어떤 object를 지칭
=> 객체 안에 정의된 함수 ( .methodName() 으로 실행하는 함수)
=> method가 아닌 모든 함수
-> 해당 method가 정의된 객체 (object)
method 정의 할 때, 반드시 function(){} 으로 정의 한다!
ex)
const obj = {
name: 'obj',
method1: function(){
console.log(this) // this = obj
},
objInObj: {
name: 'oio',
oioMethod(){ // ES6 Syntatic sugar: 코드를 짧고 쉽게 쓰게 해줌! 함수를 이렇게 짧게 정의 하게 해줌!
console.log(this) // this = objInObj
}
},
arr: [0, 1, 2],
newArr: [],
method2 () {
/*
this.arr.forEach(
// 애는 method가 아니다! 그냥 내가 만든 익명함수임
// -> this 사용 불가
function(number){
this.newArr.push(number*100)
}.bind(this) // bind해서 사용 가능하다!
// -> 그런데 이렇게 길게 쓸거니...?
// => 그게 싫어서 나온게 arrow funciton이다!
)//obj
*/
(number) => {
this.newArr.push(number * 100)
}
}
}
this
에는 eventListener가 불린 주어가 되는 애가 this
에 들어온다!
- 수동적임!
- 불려진 대상에 따라 달라진다