window === this 가 출력됨! 즉, this가 함수안에서 전역객체를 뜻하는 window와 같음을 알수있음!"> window === this 가 출력됨! 즉, this가 함수안에서 전역객체를 뜻하는 window와 같음을 알수있음!"> window === this 가 출력됨! 즉, this가 함수안에서 전역객체를 뜻하는 window와 같음을 알수있음!">
function func(){
if (window === this) {
document.write('window === this");
}
}
func();
// 전역객체 window랑 func()함수 내에서의 this가 정확하게 같다면 출력하라!
=> window === this 가 출력됨! 즉, this가 함수안에서 전역객체를 뜻하는 window와 같음을 알수있음!
그러나 객체의 소속인 메소드의 this 는 그 객체를 가리킨다
var o = {
func : function() {
if (o === this) {
document.write("o === this");
}
}
}
o.func();
var funcThis = null;
function Func() {
funcThis = this;
}
var o1 = Func(); // 함수로 호출하면 그 함수에서의 this값은 window가 됨. 전역객체인
if (funcThis === window) {
document.write('window </br>');
}
var o2 = new Func(); // 생성자의 개념으로 호출될때는 내부 this는 담아주는 o2객체가 됨
if (funcThis === o2) {
document.write('o2 </br>');
}