본문 바로가기

Dev-/java script, jQuery, Ajax

자바스크립트 this 바인딩

function Hello() {
console.log(this);
};

const testObj = {
name: 'test'
};


Hello(); /** 1. 일반적인 경우 : undefined or Window ---> '호출'한 객체 */

testObj.hello = Hello;
testObj.hello(); /** 1. 일반적인 경우 : testObj 객체 ---> '호출'한 객체 */

new Hello(); /** 2. 생성자로 쓰이는 경우 : Hello 객체 ---> 생설될 객체의 Prototype 객체 */

1. 일반적인 경우, 해당 함수를 호출한 객체에,

2. 생성자 호출의 경우, 생성될 객체의 Prototype 객체에 바인딩 된다.