전역객체 : 특수한 객체다. 모든 객체는 이 전역객체의 프로퍼티다. 모든 전역함수와 변수는 window 객체의 프로퍼티다. 객체를 명시하지 않으면 암시적으로 window의 프로퍼티로 간주된다.
예시
//
function func(){
 alert('hello?');
}
func();
window.func();

 

//
var o = {'func':function (){
 alert('hello?');
}}
o.func();
window.o.runc();

 

this : 함수 내에서 함수 호출 맥락을 의미한다. 함수와 객체의 관계를 연결시켜주는 실질적인 연결점 역할을 한다. 생성자가 실행되기 전까지는 객체는 변수에도 할당될 수 없기 때문에, this가 아니면 어떠한 작업도 할 수 없다. 함수의 메소드인 apply, call을 이용하면 this의 값을 제어 할 수 있다.
예시
//
function func(){
 if(window === this){
  document.write("window === this");
 }
}
func();

 

//
var o = {
 func : function (){
  if(o === this){
   document.write("o === this");
  }
 }
}
o.func();

 

//
var funcThis = null;
 
function Func(){
    funcThis = this;
}
var o1 = Func();
if(funcThis === window){
    document.write('window <br />');
}
 
var o2 = new Func();
if(funcThis === o2){
    document.write('o2 <br />');

}

 

//
var o = {}
var p = {}
function func(){
    switch(this){
        case o:
            document.write('o<br />');
            break;
        case p:
            document.write('p<br />');
            break;
        case window:
            document.write('window<br />');
            break;         
    }
}
func();
func.apply(o);
func.apply(p);

'Programming > Javascript' 카테고리의 다른 글

표준내장객체, Object 객체  (0) 2018.01.03
상속, prototype  (0) 2018.01.03
생성자와 new  (0) 2018.01.03
함수의 호출  (0) 2018.01.03
arguments, 매개변수의 수  (0) 2018.01.03
블로그 이미지

꼴통보안인

,