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';