Procedure Language(절차적인 언어) 같은 경우에는 함수가 프로그램 내부에서 굉장히 중요한 역할 함.
javscript는 object language 가 아니다. ES6에서 추가된 class 는 Java 언어 처럼 Pure한 object oriented가 아닌 prototype을 base한 가짜의 object oriented 이다
즉 Javascript 는 procedure language 중 하나라고 볼 수 있다.
function은 sub-program이라 부르기도 한다.
특징 : fundamental building block in the program / subprogram can be used multiple times / performs a task or calculates a value
주의 : one function === one thing / naming: doSomething, command, verb (목적을 확실) / 세분화 할 수 있으면 끝까지 쪼개라.
function is object in Js
parameters
primitive parameters : passed by value, 메모리에 Value가 저장되어 있기에 Value가 그대로 전달.
object parameters : passed by reference, reference 에 저장되어 전달되어 진다.
오브젝트는 reference로 전달되기 때문에 함수 안에서 object에 값을 변경하게 되면 그 변경된 사항이 그대로 메모리에 적용이 된다.
Default parameters (added in ES6) > function showMessgae(messgae = 'Msg') 사용자가 parameter를 전달하지 않아도 대체 되어서 사용.
Rest parameters (added in ES6) > function printAll(...args) 배열로서 파라미터가 들어간다.
현업에서의 Code는 Early return, Early exit 를 추구 함. 대충 값이 다르면 빨리 빠져나오고 뒤 코드 보지 말라는 이야기. 가독성 문제도 있음.
First-class function
> functions are treated like any other variable
> can be assigned as a value to variable. (할당)
> can be passed as an argument to other functions. (전달)
> can be returned by another function. (반환)
Function expression
> a function declaration can be called earlier than it is defined. (hoisted) / 호이스팅이 되어 선언 이전에도 사용이 가능하다.
> a function expression is created when the execution reaches it.
anonymous function > const print = function () { bla bla }
named function > const printNo = function print() { bla bla ) / better debugging in debugger's stack traces 목적
Arrow function
> always anonymous 간결하게 작성 할 수 있다.
IIFE: Immediately Invoked Function Expression
> 문법 / 선언 된 함수 블록을 ()로 묶은 후 () 사용하면 선언과 동시에 사용 가능
like this > (function hello() {console.log('IIFE')})()