• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • TIL
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BruteForce
    • DFS
    • DP
    • Optimization
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Spring
    • Node.js
    • DataBase
      • MySQL
  • Programming
    • Programming
    • Java
    • Python
  • ComputerScience
    • DataStructure
    • Algorithm

[노드교과서] 2장. ES2018 (5) 비구조화 할당

05 Mar 2020

Reading time ~1 minute

이 포스트는 ZeroCho님의 저서 Node.js 교과서와 인프런 강의를 기반으로 작성한 글입니다.
  • 비구조화 할당 사용 전
    • 비구조화 할당
  • 비구조화 할당 사용 후
    • 화살표 함수 축약 형태
  • this가 동작하는 방식
    • function 사용 시 this
    • 화살표함수 사용 시 this

비구조화 할당 사용 전

비구조화 할당

var candyMachine = {
    status:{
        name: 'node',
        count: 5,
    },
    getCandy: function(){
        this.status.count--;
        return this.status.count;
    }
};

var status = candyMachine.status

var getCandy = candyMachine.getCandy

비구조화 할당 사용 후

const candyMachine = {
    status:{
        name: 'node',
        count: 5,
    },
    getCandy: function(){
        this.status.count--;
        return this.status.count;
    }
};

const status = candyMachine.status;
const getCandy = candyMachine.getCandy;
const candyMachine = {
    status:{
        name: 'node',
        count: 5,
    },
    getCandy: function(){
        this.status.count--;
        return this.status.count;
    }
};

const {status, getCandy} = candyMachine;

화살표 함수 축약 형태

함수 내부가 return 밖에 없을 경우 아래와 같이 줄여서 사용할 수 있습니다.

const add4 = (x,y) => x+y;

this가 동작하는 방식

function 사용 시 this

var relationship1 = {
    name: 'zero',
    friends: ['nero','hero','xero'],
    logFriends:function(){
        var that = this;
        this.friends.forEach(function(friend){
            console.log(that.name, friend);
        });
    },
};
relationship1.logFriends();
'''
zero nero
zero hero
zero xero
'''

function을 사용할 경우 this가 가리키는 것은 logFriends가

화살표함수 사용 시 this

var relationship2 = {
    name: 'zero',
    friends: ['nero','hero','xero'],
    logFriends(){
        var that = this;
        this.friends.forEach(friend => {
            console.log(that.name, friend);
        });
    },
};
relationship2.logFriends();
'''
zero nero
zero hero
zero xero
'''


노드교과서 Share