억삼이 2019. 1. 31. 11:24
반응형

함수의 this binding!!


* 함수가 호출될 때마다 this binding!!

* Lexical this binding!

   - default : 현재 호출중인 메서드를 보유한 객체가 this로 연결됨.


var obj = {

   name : "홍길동",

   getName : function() {

      return "이름 : " + this.name;

   }

}


obj.getName()


var obj2 = { name:"강사만세!!" }

obj2.getName = obj.getName;


obj2.getName === obj.getName

obj2.getName();


-------------------------------------

<<this 직접 바인딩>>


var obj = {

   name : "홍길동"

}


function getName(msg) {

   return "이름 : " + this.name + "님! " + msg;

}


//getName("안녕");

//getName.apply(obj, ["안녕"])

var test1 = getName.bind(obj);

test1("안녕");

--------------------------------------

<<함수의 중첩구조와 this>>


- 전통적인 함수 : lexical this binding!! (함수의 호출형태가 중요)

- arrow function : 바깥쪽 함수의 this가 자동으로 arrow function의 this로 미리 바인딩됨.


var obj = {

   name : "홍길동",

   getName : function() {

      var inner = () => {

         console.log(this);

         return "이름 : " + this.name;

      }

      return inner(); 

   }

}


obj.getName();


\\70.12.116.160


1. pass primitive values via props

   https://codesandbox.io/s/0427qowpxp


2. pass object via props

   https://codesandbox.io/s/mo9o0x99lp


https://www.a-ha.io/questions/4a79b6210027488a9b46c2810fc8b39a





반응형