ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Function
    개발/Javascript 2022. 6. 3. 22:54

    *복습 자료라서 뻔한 내용은 생략

     

     

    Summary.

     

     

    1. Function Declaration 과 Function Expression 으로 나뉨 (*Declaration만 Code Hoisting 적용됨)

        ex) function Foo(){}

        ex) var fooFunc = function () {}; // anonymous

        ex) var fooFunc = function Foo(){}; // debug-friendly

     

    2. Function Parameter는 Object, Array 인 경우 Pass by Reference로 변경 가능 (기본은 Pass by Value)

        ex) data.name= "hello";

        ex) data[0] = 34;

     

    3. Function은 Object 일종 (Function Object)

        ex) (function(){}).constructor === Function

     

    4. Function Scope: Outer Function Variables 또는 Global Variables 접근가능. Nested Function은 외부에서 접근 못함

        + Scope Chaining 법칙에 따라 Outermost Function Variables 접근가능 (함수 A 안의 함수 B 안의 함수 C는 A 함수 내 변수 접근 가능)

     

    5. Closure는 일종의 Expression (대개 nested function). Inner(nested) Function은 자체 environment 가지고, Outer Function Variable 혹은 Parameter에 접근가능한 특성을 활용

    function outside(x) {
      function inside(y) {
        return x + y;
      }
      return inside;
    }
    fn_inside = outside(3); 
    result = fn_inside(5); // returns 8
    result1 = outside(3)(5); // returns 8
    // 생성될 때마다 다른 x가 저장된 inside 함수가 탄생함. inside 함수가 사라져야 x 메모리할당이 반환됨

     

    6. Outer Function 에서 Inner Function 내 Variables 접근 불가하다는 특성은 Encapsulation (+ Persistence)

    var getCode = (function() {
      var apiCode = '0]Eal(eh&2';    // A code we do not want outsiders to be able to modify...
    
      return function() {
        return apiCode;
      };
    })();
    
    getCode();    // Returns the apiCode

     

    7. arguments Object를 활용하면 variable length arguments 대응 가능

    function myConcat(separator) {
       var result = ''; // initialize list
       var i;
       // iterate through arguments
       for (i = 1; i < arguments.length; i++) {
          result += arguments[i] + separator;
       }
       return result;
    }
    
    // returns "red, orange, blue, "
    myConcat(', ', 'red', 'orange', 'blue');

     

    8. ECMAScript 2015 이후 신규 Parameter 추가: Default, Rest 

    // Default
    function multiply(a, b = 1) {
      return a * b;
    }
    
    var result = multiply(5);
    
    // Rest
    function multiply(multiplier, ...theArgs) {
      return theArgs.map(x => multiplier * x);
    }
    
    var arr = multiply(2, 1, 2, 3);

     

    9. Arrow Function은 Anonymous Function Expression 이며, Motivation은 shorter functions & non-binding of this

        ex) strict mode에서 어떤 anonymous function 내 this는 outer function이 아닌 global this를 가리키는 경우가 있었음

        ex) 이를 명확히 구분하기 위해 var self = this; 혹은 bind 함수, 등을 활용했었음

        + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#no_separate_this

        

    10. Pre-defined Functions

        + eval(), isFinite(), isNaN(), parseInt(), parseFloat(), encodeURI(), decodeURI(), escape(), 등

        

    Reference.

    '개발 > Javascript' 카테고리의 다른 글

    Module System  (0) 2022.06.05
    Prototype Chain  (0) 2022.06.05
    Memory Management  (0) 2022.06.05
    Array & Loop  (0) 2022.06.04
    Type  (0) 2022.06.04
Designed by Tistory.