ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [ES6] Arrow Function
    Front/JavaScript 2020. 7. 31. 16:41

    1)

    - 정적으로, 선언시 this가 정의된다. <-> 리터럴은 동적으로, 호출시 정의된다(감싼 객체)

    - this는 가까운 non-arrow 펑션의 this를 참조한다.

    var literal = function(){console.log(this)}; 	// Window
    var arrow = ()=>{console.log(this)};		// Window
    
    var obj = {
    	literal : literal,			// Object
       	arrow : arrow				// Window
    };
    
    
    var obj = {
        outer(){
            literal();				// Window
            arrow();				// Window
        
            (function(){console.log(this)})();	// Window
            (()=>{console.log(this);})();		// Object
        }
    };
    
    var obj = {
        outer: () => {
            (()=>{console.log(this);})();		// Window
        }
    };

     

    2) 함수안에 arguments 변수가 없다

    var literal = function(){console.log(arguments)};
    var arrow = ()=>{console.log(arguments)};
    
    
    literal();		// Arguments
    literal(1,2);		// 1,2
    
    arrow();		// Uncaught ReferenceError: arguments is not defined
    arrow(1,2);
    

     

    3) 생성자로 사용할 수 없다

    var Plus = (x, y) => console.log(x + y);
    
    new Plus(2,3);		// Plus is not a constructor
    

     

    4) strict or non-strict 변수이름 중복 불가

    (x, x) => console.log(x + x);	// duplicate argument names
    

     

    5) 함수안에서 yield 키워드 사용불가 (제너레이터 불가)

    'Front > JavaScript' 카테고리의 다른 글

    [JS] FileReader  (0) 2020.07.29
    [JS] 정규식 RegExp  (0) 2020.07.29
    [ES6] arrow function this  (0) 2020.06.27
Designed by Tistory.