자바스크립트 this 바인딩


자바스크립트 this 바인딩

#자바스크립트 #this바인딩 this는 봐도 봐도 헷갈리기 때문에 정리를 한 번 해놓아야 겠다. 자바스크립트에서 함수를 호출할 때 인자 값에 더해 arguments 객체 및 this 인자가 함수 내부로 전달된다. 자바스크립트의 여러 가지 함수가 호출되는 방식에 따라 this의 바인딩도 달라진다. 1. 객체의 메서드 호출할 때 this 바인딩 : 메서드 내부 코드에서 사용된 this는 해당 메서드를 호출한 객체로 바인딩 된다. var myObj = { name : 'foo', sayName : function() { console.log(this.name); } }; myObj.sayName(); // foo 2. 함수를 호출할 때 this바인딩 : 함수 내부 코드에서 사용된 this는 전역 객체에 바인딩 된다. (브라우저의 경우 window객체) var test = 'This is test'; console.log(window.test); // This is test var say...


#this바인딩 #자바스크립트

원문링크 : 자바스크립트 this 바인딩