본문 바로가기

함수5

JavaScript기초(19)_함수(5) 1. () => {} : arrow function 기본적인 형태를 살펴보자. const hello = () => { console.log('hello!'); }; 매개변수 하나를 추가해보자. const hello2 = name => { console.log('hello!', name); }; 매개변수가 하나일 때는 '()' 생략 가능하다. 매개변수가 2개 이상일 때는 다음과 같다. const hello2 = (name, age) => { console.log('hello!', name, age); }; 매개변수가 2개 이상일 때는 반드시 '()'를 써야 한다. 함수의 리턴하는 방법을 살펴보자. const hello1 = (name) => { return `hello1 &{name}`; }; const .. 2021. 10. 13.
JavaScript기초(18)_함수(4) 1. const hello = new Function(); Function 안에는 각 인자들과 함수의 바디를 아래와 같이 넣을 수 있다. // new Function( /* 인자1, 인자2, ... , 함수의 바디 */); const sum = new Function('a', 'b', 'c', 'return a + b + c'); console.log(sum(1,2,3)); [Running] 6 인자의 인식 범위에 대하여 다음과 같은 코드들을 통해 알 수 있다. { const a = 1; const test = new Function('return a'); console.log(test()); } [Running] return a ^ ReferenceError: a is not defined 변수 a가 .. 2021. 10. 12.
JavaScript기초(17)_함수(3) 1. 선언적 function과 익명 함수의 변수에 할당하는 표현방식의 차이 우선 아래의 코드를 살펴보자. 함수 호출을 먼저 하고 뒤에 선언적 function을 입력했다. hello(); function hello(){ console.log('hello!'); } [Running] hello! 선언을 뒤에 하고 호출을 먼저하더라도 함수 결과 값이 정상적으로 함수 호출이 된다. 그렇다면 변수에 함수를 할당한 경우는 어떤지 살펴보자. hello(); var hello = function (){ console.log('hello!'); }; [Running] hello(); ^ TypeError: hello is not a function 에러 메시지를 통해서, 먼저 호출한 hello를 함수로 인식하지 못함을 .. 2021. 10. 11.
JavaScript기초(16)_함수(2) 1. const hello = function() {} : 익명적 함수를 만들어 변수에 할당 아래와 같이 함수를 변수 화하여 나타낼 수 있다. const hello = function(){ console.log('hello'); }; console.log(hello); [Running] [Function: hello] 매개변수가 있더라도 크게 다르지 않다. const hello2 = function(name){ console.log('hello', name); }; const hello3 = function(name){ return `hello3 ${name}`; }; 2021. 10. 10.