자바스크립트 함수

목차


  • Functions
  • Traditional Function
  • Arrow Function
  • Conclusion

  • 기능

    A fundamental building block for Javascript programs. The most common feature in all programming languages. What exactly is a function in technical terms? a function is a block of code that is defined once and can be executed or invoked numerous times. Another quick note is functions are also objects, meaning they can be manipulated by other programs too.

    We can declare a function by using the function keyword, following by a name of our liking, followed by a pair of opening and closing parentheses , a space for between the parameters and the curly braces with zero or more statements inside.

      function buzz() { }
    

    There is another way of declaring functions in modern javascript, and thats with the arrow function . Which actually looks like an arrow, but does have its own section that we will get too since it has limitations.

      () => {}
    

    Inside of the parentheses we can include identifiers or in other words parameters, these parameters act as local variables for the body of the function. To get a value out of those variables we can simply invoke our function by including some values or in technical terms arguments. We use these arguments to compute a value and return that value part of the invocation.

    If a function is assigned to a property of an object, that function becomes a method of that object. This is where things start to get a bit more complex since we are now invoking on or through an object, as where the object becomes the context of the this keyword. The this keyword is a while other article to discuss about so we will skip that for now, but it's something good to note down about when creating methods for objects. With that being said we are also allowed to simply assign a function to a variable and pass them to other functions. Since functions are objects like we mentioned before, you can set properties on them and invoke methods on them too.

    전통적인 기능


    function 키워드는 함수를 선언하는 유일한 방법입니다. 우리는 또한 거의 함수가 변수 내부에 저장된 함수 표현식을 선언할 수 있습니다. 예를 들어

    함수 선언

    
    function printName(str) {
       return "Hello " + str
    }
    
    console.log(printName('oscar')) // output "Hello oscar"
    
    


    함수식

    let addUp = function(a,b) {
      return a + b
    } 
    
    console.log(addUp(1,2)) // output 3
    

    function declarationfunction expression의 주요 차이점 중 하나는 함수 이름으로, 함수 표현식에서 익명 함수를 생성하기 위해 생략할 수 있습니다. 기본적으로 함수는 반환할 값이 있는 특정 반환 문이 없는 경우 undefined를 반환합니다. 함수에 대해 주의해야 할 또 다른 사항은 if/else 문 내에서도 조건부로 선언될 수 있지만 일반적으로 결과가 일관되지 않으므로 사용하기에 좋은 패턴이 아니라는 것입니다.

    선언 함수와 함께 제공되는 멋진 기능은 호이스팅입니다. 함수를 선언하기 전에 사용할 수 있습니다.

    printHello(); // logs 'Hello'
    
    function printHello() {
       console.log('Hello');
    }
    
    


    반면에 함수 표현식은 호이스팅되지 않습니다. 따라서 함수 표현식을 생성하기 전에는 사용할 수 없습니다.

    화살표 함수



    우리는 ES6에서 arrow functions에 소개되었습니다. 이것은 전통적인 함수 표현에 대한 훌륭한 간결한 대안이지만 제한 사항이 있고 모든 상황에서 사용할 수는 없습니다. this 또는 super 키워드와 같은 바인딩이 없으며 call , apply 또는 bind 메서드에 적합하지 않으며 생성자로도 사용할 수 없습니다.
    declared functionarrow function로 분해합시다.

      function(name) {
        return 'Hello ' + name;
      }
    


    먼저 function 키워드를 제거하고 인수를 그대로 두고 인수와 중괄호 사이에 arrow를 추가합니다.

      (name) => {
      return 'Hello ' + name;
    }
    


    다음으로 중괄호와 return 문을 제거합니다. return 문은 자동으로 암시됩니다.

      (name) => 'Hello ' + name;
    


    마지막 단계에서는 매개변수에 전달되는 인수가 하나뿐이므로 괄호를 제거할 수 있습니다.

      name => 'Hello ' + name;
    


    이전에 언급했듯이 버그 또는 기타 문제를 방지하기 위해 화살표 함수methods를 사용하지 않는 것이 좋습니다. 화살표 함수는 메서드가 아닌 함수에 가장 적합합니다.

    결론

    I hope by the end of this article you managed to learn how to create and understood what is going on in every line of code. It is very important to understand how your code fully works, not only does it help you become a better developer but can also help you use the tools you are working with more efficient.

    These articles are mostly intended for personal use on becoming a better programmer, writer, and grow my programming skills. Feel free to drop any feedback or corrections that you believe that should be made to help me and others. Thank you for your time for sticking this far!

    좋은 웹페이지 즐겨찾기