JaeYeongSong
Xsop의 개발일기
JaeYeongSong
전체 방문자
오늘
어제
  • 분류 전체보기 (41)
    • 💻 Python (17)
      • Python 기초 강의 (6)
      • 비트코인 자동 매매 (7)
      • 크롤링 (3)
      • 알고리즘 (1)
    • ✨ JavaScript (9)
      • Basic (8)
      • Node.js (1)
    • ⭐ HTML (9)
    • 🌟 API (2)
    • 💡 일상 (3)

블로그 메뉴

  • 홈
  • 태그
  • 게시물

공지사항

인기 글

태그

  • Web
  • slack api
  • callback
  • input
  • 태그
  • 글쓰기
  • 지정가
  • 자료형
  • 동기
  • Python
  • HTML
  • 웹
  • Web page Crawling
  • 자산 조회
  • 웹 사이트
  • Slack
  • 비동기
  • 비트코인 자동매매
  • JavaScript
  • 크롤링
  • 기초 문법
  • API
  • Crawling
  • 시장가
  • 웹 페이지 크롤링
  • 봇
  • 비트코인
  • upbit
  • 문자열
  • upbit api

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
JaeYeongSong

Xsop의 개발일기

✨ JavaScript/Basic

[JavaScript] 화살표 함수(this 바인딩) 알아보기

2021. 11. 4. 21:27

화살표 함수

Code

function add1(x, y) {
    return x + y;
};

const add2 = (x, y) => {
    return x + y;
};

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

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

function not1(x) {
    return !x;
};

const not2 = x => !x;

화살표 함수를 사용하면 코드를 짧게 줄일 수 있다.


사용법

  • 함수를 선언할 때 function 선언문 대신 ⇒ 기호로 함수 선언
// function
function add(x, y) {
	return x + y;
}

// 화살표 함수
const add = (x, y) => {
	return x + y;
}
  • 함수 내부에 return문 밖에 없다면 ⇒ 기호 뒤에 return 할 식 적기
    (매개변수가 1개라면 소괄호로 묶어주지 않아도 됨.)
// return
const add = (x, y) => (x + y);
const add = (x, y) => x + y;

// 매개변수 1개
const not = x => !x;

this 바인딩

Code

var relationship1 = {
    name: 'zero',
    friends: ['nero', 'hero', 'xero'],
    logFriends: function () {
        var that = this; // relationship1을 가리키는 this를 that에 저장
        this.friends.forEach(function (friend) {
            console.log(that.name, friend);
        });
    },
};
relationship1.logFriends();

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

화살표 함수를 사용하면 상위 스코프의 this를 그대로 물려받을 수 있다.


저작자표시 (새창열림)
    '✨ JavaScript/Basic' 카테고리의 다른 글
    • [JavaScript] async, await (Promise를 간결하고 깔끔하게!)
    • [JavaScript] Promise(프로미스) Callback 지옥을 해결하는 방법!
    • [JavaScript] Class 알아보기
    • [JavaScript] 변수 선언자 var, let, const 차이점
    JaeYeongSong
    JaeYeongSong
    "I walk slowly, but I never walk backwards. - Abraham Lincoln" "나는 천천히 걷지만 결코 뒤로 걷지 않는다. - 에이브러햄 링컨" #코딩 #Python #JavaScript

    티스토리툴바