메소드 호출시 this바인딩

CODEDRAGON Development/JavaScript, jQuery, ...

반응형

메소드 호출시 this바인딩

   

객체의 프로퍼티가 함수일 경우, 이함수를 메소드라고 합니다.

이 메소드를 호출할 때, 메소드 내부 코드에서 사용된 this는 해당 메소드를 호출한 객체로 바인딩됩니다.

   

   

소스 코드

<html>

<head>

<meta charset="UTF-8">

<title>메소드 호출시 this바인딩</title>

<script type="text/javascript">

//thisObject 객체 생성

var thisObject = {

name: 'Object1',

getName: function () {

// this 자신을 호출한 객체에 바인딩됩니다.

document.write(this.name +'<br>');

}

};

// ohterObject 객체 생성

var otherObject = {

name: 'Object2'

};

// otherObject.getName() 메서드

otherObject.getName = thisObject.getName;

// getName() 메서드 호출

//thisObject객체 에서 호출되었으므로, 이메소드의 this thisObject객체를 가리킵니다(바인딩됩니다).

thisObject.getName();

//otherObject객체에서 호출되었으므로, 이메소드의 this otherObject객체를 가리킵니다(바인딩됩니다).

otherObject.getName();

</script>

</head>

<body>

</body>

</html>

   

출처: <https://github.com/10zeroone/study_javascript/blob/master/WebContent/ch04-function/31.html>

   

   

   

this 호출 도식도

반응형

'Development > JavaScript, jQuery, ...' 카테고리의 다른 글

객체 (Object)  (0) 2014.08.19
전역객체, 전역변수, this바인딩  (0) 2014.08.17
arguments 객체 이용  (0) 2014.08.08
arguments 객체  (0) 2014.08.06
매개변수  (0) 2014.08.04