this

김_리트리버·2020년 9월 28일
0

this in javascript

this keyword refers to an object, that object which is executing the current bit of javascript code.

= this 는 현재 자바스크립트 코드의 bit 를 실행하는 객체다.

= 함수의 실행컨텍스트 객체

In other words, every javascript function while executing has a reference to its current execution context, called this. Execution context means here is how the function is called.

= 모든 자바스크립트의 함수는 실행할 때 this 라는 현재 실행 컨텍스트의 참조를 갖는다.

To understand this keyword, only we need to know how, when and from where the function is called,

= 함수가 언제 어디서 호출되는지만 신경쓰면 된다.

  • this 는 함수를 호출하는 방법에 의해 결정된다.

  • this : 함수를 호출한 객체


let people ={

    name : 'kim',

    whoAmI : function(){

        console.log(this);
    }

}

people.whoAmI();

// {name: "kim", whoAmI: ƒ}

let person = people.whoAmI;

person();

// Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
  • global 에서 호출 -> this : window
  • function에서 호출 -> this : window
  • method -> this : 함수를 호출한 객체
  • constructor -> this : 자기자신
  • call,apply -> 첫번째 전달인자로 넘겨주는 객체
profile
web-developer

0개의 댓글