1)
var o ={
func : function(){
if(0===this){
document.write("o===this") //o===this가 출력됨.
}
}
}
2)
function func(){
if(window)===this){ //--> 일반적인 객체에 속해있지 않은 this
console.log("window===this"); //window===this가 출력됨.
}
}
결론: this는 함수 안에서 사용되는 일종의 변수이다.
객체 안에서의 this는 'this가 속한 객체(자기자신)'를 가리킨다. 만약 this가 객체 밖에 있다면, Window라는 전역 객체를 가리킨다.
o.func() 객체에서 this는 o(현재 this가 속해있는 곳)을 가리킨다. 어떤 곳에서도 속하지 않은 모든 전역 객체/변수는 기본적으로 window에 속해있다. 따라서 객체 밖에 있는 this는 모든 전역 객체/변수는 기본적으로 속해있는 window를 가리킨다.
(모든 전역 객체 앞에는 window가 생략되어 있다. -> (window)func()
p.s) *Property(속성)값이 함수인 것을 메소드라고 특별히 부름
객체(Object), 속성(Property), 키(Key), 값(Value)를 정리한 내용은 아래 글로 이동.
https://box-cat.tistory.com/162
자바스크립트) 객체(object)-속성(property)-키(key)-값(value)
const John = { age: 21, nation: America, sex: male, height: 175cm, weight: 65kg }; 객체(object): 위의 {}덩어리 Property(속성): 속성=키:값 age: 21,nation: America, sex: male, height: 175cm, weigh..
box-cat.tistory.com
'개발공부 일지 > 자바스크립트' 카테고리의 다른 글
자바스크립트) 마우스 클릭 좌표(screenX, clientX, pageX, offsetX) (0) | 2021.08.25 |
---|---|
자바스크립트) event.cancelBubble is not a function 오류발생 시 해결법. (0) | 2021.08.10 |
자바스크립트)window.getComputedStyle 와 getPropertyValue('') (0) | 2021.08.05 |
자바스크립트) 객체(object)-속성(property)-키(key)-값(value) (0) | 2021.07.29 |
자바스크립트) 소수판별 스크립트 (0) | 2021.07.15 |
댓글