화살표 함수는 함수 선언문으로 정의할 수 없고 함수 표현식으로 정의해야한다.
호출 방식은 기존 함수와 동일하다.
const multiply = (x,y) => x*y;
multiply(2,3); //6
const arrow = (x,y) => { ... };
const arrow = x => {...};
const arrow = () => {...};
//concise body (간결한 표현)
const power = x => x **2;
power(2); // 4
// 위 표현은 다음과 동일
const power = x => {return x ** 2;};
const arrow = () => const x = 1; // SyntaxError : Unexpected token 'const'
// 위 표현은 다음과 같이 해석된다.
const arrow = () => {return const x = 1;};
const arrow = () => { const x = 1;};
const create = (id, content) => ({ id, content });
create(1, 'Javascript'); // {id: 1, content: "Javascript"}
// 위 표현은 아래와 동일하다
const create = (id, content) => {return {id,content};};
const create = (id,content) => {id, content};
create(1, "Javascript"); // undefined
const sum(a,b) => {
const result = a+b;
return result;
};
const person = (name =>({
sayHi(){ return `Hi? My name is ${name}.`;}
}))("Park");
console.log(person.sayHi()); // Hi? My name is Park.
//ES6
[1,2,3].map(v => v*2); // [2,4,6]
const Foo = () => {};
// 화살표 함수는 생성자 함수로서 호출할 수 없다.
new Foo(); // TypeError: Foo is not a constructor
const Foo = () => {};
//화살표 함수는 prototype 프로퍼티가 없다.
Foo.hasOwnProperty('prototype'); // false
function normal(a,a) {return a+a;}
console.log(normal(1,2)); //4
'use strict';
function normal(a,a) {return a + a; }
// SyntaxError: Duplicate parameter name not allowed in this context
const arrow = (a,a) => a+a;
//SyntaxError: Duplicate parameter name not allowed in this context