arrow function, class - JeongWu/fe-javascript GitHub Wiki
Arrow function
์ ์ธ
// ๋งค๊ฐ๋ณ์ ์ง์ ๋ฐฉ๋ฒ
() => { ... } // ๋งค๊ฐ๋ณ์๊ฐ ์์ ๊ฒฝ์ฐ
x => { ... } // ๋งค๊ฐ๋ณ์๊ฐ ํ ๊ฐ์ธ ๊ฒฝ์ฐ, ์๊ดํธ๋ฅผ ์๋ตํ ์ ์๋ค.
(x, y) => { ... } // ๋งค๊ฐ๋ณ์๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ, ์๊ดํธ๋ฅผ ์๋ตํ ์ ์๋ค.
// ํจ์ ๋ชธ์ฒด ์ง์ ๋ฐฉ๋ฒ
x => { return x * x } // single line block
x => x * x // ํจ์ ๋ชธ์ฒด๊ฐ ํ์ค์ ๊ตฌ๋ฌธ์ด๋ผ๋ฉด ์ค๊ดํธ๋ฅผ ์๋ตํ ์ ์์ผ๋ฉฐ ์๋ฌต์ ์ผ๋ก return๋๋ค. ์ ํํ๊ณผ ๋์ผํ๋ค.
() => { return { a: 1 }; }
() => ({ a: 1 }) // ์ ํํ๊ณผ ๋์ผํ๋ค. ๊ฐ์ฒด ๋ฐํ์ ์๊ดํธ๋ฅผ ์ฌ์ฉํ๋ค.
() => { // multi line block.
const x = 10;
return x * x;
};
ํน์ง
- ์ต๋ช ํจ์
//ํจ์ ํํ์
const pow = x => x * x;
console.log(pow(10)); // 100
//์ฝ๋ฐฑ
const arr = [1, 2, 3];
const pow = arr.map(x => x * x);
console.log(pow); // [ 1, 4, 9 ]
this ๋ฐ์ธ๋ฉ
- ์์ ์ this, arguments, super ๋ฐ์ธ๋ฉ
- ํ์ดํ ํจ์์ this ์ธ์ ๋ ์์ ์ค์ฝํ์ this๋ฅผ ๊ฐ๋ฆฌํด
- ํจ์์์ this ๋ฐ์ธ๋ฉ
function Person() {
this.age = 0;
setTimeout(function growUp() {
console.log("growUp", this, this.age);
}, 1000);
}
var p = new Person();
// growup / Window / NaN
- Es5
function Person() {
var that = this;
that.age = 0;
setTimeout(function growUp() {
// ์ฝ๋ฐฑ์ `that` ๋ณ์๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
that.age++;
console.log("growUp", this, that, that.age);
}, 1000);
}
const p = new Person();
// growup / Window / Person,1
- Es6
function Person() {
this.age = 0;
setTimeout(()=>{
this.age++;
// this๋ person ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ
console.log(this, this.age);
}
, 1000);
}
var p = new Person();
// Person {age: 1} 1
- ์์ฑ์๋ก์ ์ฌ์ฉํ ์ x
ํ์ฉ
- forEach: ๋ฐฐ์ด์ํํ๋ฉฐ callback ์คํ
const array = [ 1,2,3,4,5,6,7,8,9,10 ];
array.forEach(n => console.log(n*n));
- map: ๋ฐฐ์ด์ํํ๋ฉฐ callback ๊ฒฐ๊ณผ ๊ฐ์ ์๋ก์ด ๋ฐฐ์ด ๋ฐํ
const array = [ 1,2,3,4,5,6,7,8,9,10 ];
console.log(array.map(n => n * n)); //(10) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
- filter: ๋ฐฐ์ด์ํํ๋ฉฐ callback ๊ฒฐ๊ณผ๊ฐ์ด true์ธ ์์๋ง ๋ด์ ์๋ก์ด ๋ฐฐ์ด ๋ฐํ
const array = [ 1,2,3,4,5,6,7,8,9,10 ];
console.log(array.filter(n => n % 2)); //(5)[1, 3, 5, 7, 9]
- reduce: ๋ฐฐ์ด์ํํ๋ฉฐ callback ๊ฒฐ๊ณผ๊ฐ ๋ฐํ
const array=[1,2,3,4,5,6,7,8,9,10];
console.log(array.reduce((a, b) => a + b, 0)); // 55
- every: ๋ฐฐ์ด์ํํ๋ฉฐ callback์ ๊ฒฐ๊ณผ bool๊ฐ์ด ๋ชจ๋ true์ผ ๊ฒฝ์ฐ true ๋ฐํ
const array = [ 1,2,3,4,5,6,7,8,9,10 ];
console.log(array.every(n => n > 0)); // true,
console.log(array.every(n => n > 4)); // false,
- some: ๋ฐฐ์ด์ ์ํํ๋ฉฐ callback ๊ฒฐ๊ณผ bool๊ฐ์ ํ๋๋ผ๋ true์ผ ๋ true
const array = [ 1,2,3,4,5,6,7,8,9,10 ];
console.log(array.some(n => n > 100)); // true,
console.log(array.some(n => n > 4)); // false,
- sort: ๋ฐฐ์ด ์ ๋ ฌ(a, b: a๊ฐ ์์ผ๋ฉด -1 ๊ฐ์ผ๋ฉด 0 ํฌ๋ฉด 1)
[100, 6, 89, 34, 0, 1].sort((a, b) => a < b ? -1 : a > b);
//(6) [0, 1, 6, 34, 89, 100]
Classes
1. ํด๋์ค
- ๊ธฐ๋ณธ ๋ฐฉ์
function Item(title='no name') {
this._title = title;
}
Item.prototype = {
getTitle: function() {
return this._title;
},
setTitle: function(title) {
this._title = title;
}
}
const item = new Item();
console.log(item.getTitle());
// no name
item.setTitle('new title');
console.log(item.getTitle());
// new title
- Es6 class ์ด์ฉ ๋ฐฉ์
class Item {
constructor(title='no name') {
this._title = title;
}
get title() {
return this._title;
}
set title(title) {
this._title = title;
}
}
const item = new Item();
console.log(item.title);
// no name
item.title = 'new title';
console.log(item.title);
// new title
2. class์ body
constructor ์์ฑ์
: ๊ฐ์ฒด ์์ฑ๊ณผ ์ด๊ธฐํ
- super ํค์๋๋ก ๋ถ๋ชจ ํด๋์ค์ constructor ํธ์ถ ๊ฐ๋ฅ
prototype method
- getter, setter method
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10,10);
console.log(square.area);
// 100
static method
ํด๋์ค์ ์ธ์คํด์คํ ์์ด ํธ์ถ ๊ฐ๋ฅ
Module
์ผ์ ํ ๊ธฐ๋ฅ์ ์ํํ๊ธฐ ์ํ ์ฝ๋์ ์งํฉ
export import
export
์ธ๋ถ ์ ๊ทผ์ด ๊ฐ๋ฅํ ๋ณ์ ๋ฐ ํจ์, ํด๋์ค๋ฅผ ์ง์
- named exports: ์ฌ๋ฌ๊ฐ์ export ํ๋๋ฐ ์ ์ฉ
// module "lib.js"
export function cube(x) {
return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export { foo };
export foo; //unexpected token error
export {cube};
//export { cube, foo }; //์ค๊ดํธ๋ฅผ ์ด์ฉํด ํ๋ฒ์ export ๊ฐ๋ฅ
// main.js
import {cube, foo} from 'lib.js';
console.log(cube(3)); // 27
- default exports: ์คํฌ๋ฆฝํธ ๋น ํ๋
// "lib.js"
let cube = function cube(x) {
return x * x * x;
}
export default cube;
// main.js
import cube from './lib';
console.log(cube(3)); // 27
- ๋ ๋ค
// module "lib.js"
export default function cube(x) {
return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export { foo };
export const bar='baba'
// main.js
import cube, {bae, foo} from 'lib.js';
import
exportํ ํจ์, ํด๋์ค ๋ฑ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
- ๊ตฌ๋ฌธ
import { range, Point, RADIAN } from './utils/util'; //default export๊ฐ ์๋๋
import Point from './utils/Point'; //default export์ผ ๋
- alias
import { RADIAN as RAD } from './utils/util';
- ๋ชจ๋ namespace ๊ฐ์ฒด
impmort * as util from './utils/util';