React - The Complete Guide

Section 02: JavaScript Refresher - Next Generation JavaScript

olivia_yj 2022. 11. 9. 20:43

The goals

๐Ÿ’ช๐ŸปReview Core Knowledge of JavaScript

 

 

Let & Const

Arrow Functions

Exports & Imports (Modules)

Classes

Classes are blueprint for JavaScript objects and there are some important keywords we should remember.

Firstly, when we extend the class we need super() to apply parent class into child class. Otherwise, we will see the error sign.

Second, if we define the property already used in parent class, we can write over the same property in child class.

In our example codes, this.gender is overwritten as 'female'.

 

Applying new generation JavaScript syntax

 

Spread & Rest Operators

 

Destructuring

Reference vs Primitive types

 

Refreshing Array Functions

์ฐจ์„ธ๋Œ€ JavaScript - ์š”์•ฝ

์ด ๋ชจ๋“ˆ์—์„œ, ์ €๋Š” ๋ช‡๋ช‡ ํ•ต์‹ฌ ์ฐจ์„ธ๋Œ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋Šฅ๋“ค์— ๋Œ€ํ•œ ๊ฐ„๋žตํ•œ ์†Œ๊ฐœ๋ฅผ ํ•ด ๋“œ๋ ธ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด ๊ณผ์ •์—์„œ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์ž์ฃผ ๋ณด์‹œ๊ฒŒ ๋  ๊ฒƒ๋“ค์— ์ดˆ์ ์„ ๋งž์ถ”์—ˆ์ฃ . ์—ฌ๊ธฐ ๊ฐ„๋žตํ•œ ์š”์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค!

let & const

let ์— ๋Œ€ํ•ด ๋” ์ฝ์–ด๋ณด๊ธฐ: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

const์— ๋Œ€ํ•ด ๋” ์ฝ์–ด๋ณด๊ธฐ:: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

let ๊ณผ const ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ var ๋ฅผ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ var ๋Œ€์‹  let ์„ ์‚ฌ์šฉํ•˜๊ณ , var  ๋Œ€์‹  const๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด "๋ณ€์ˆ˜"๋ฅผ ๋‹ค์‹œ ํ• ๋‹นํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์— ๋ง์ด์ฃ  (๋”ฐ๋ผ์„œ ํšจ๊ณผ์ ์œผ๋กœ constant๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค).

ES6 Arrow Functions

๋” ์ฝ์–ด๋ณด๊ธฐ: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Arrow function์€ JavaScript ํ™˜๊ฒฝ์—์„œํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋” ์งง์€ ๊ตฌ๋ฌธ ์™ธ์—๋„ this ํ‚ค์›Œ๋“œ์˜ ๋ฒ”์œ„๋ฅผ ์œ ์ง€ํ•˜๋Š”๋ฐ ์žˆ ์ด์ ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค (์—ฌ๊ธฐ๋ฅผ ๋ณด์„ธ์š”).

Arrow function ๊ตฌ๋ฌธ์€ ๋‚ฏ์„ค๊ฒŒ ๋ณด์ผ ์ˆ˜ ์žˆ์œผ๋‚˜ ์‚ฌ์‹ค ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

  1. function callMe(name) {
  2.     console.log(name);
  3. }

๋˜ํ•œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. const callMe = function(name) {
  2.     console.log(name);
  3. }

์ด๋ ‡๊ฒŒ ๋ฉ๋‹ˆ๋‹ค:

  1. const callMe = (name) => {
  2.     console.log(name);
  3. }

์ค‘์š”: 

arguments๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, ํ•จ์ˆ˜ ์„ ์–ธ์‹œ ๋นˆ ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

  1. const callMe = () => {
  2.     console.log('Max!');
  3. }

์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ argument๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. const callMe = name => {
  2.     console.log(name);
  3. }

value๋ฅผ returnํ•  ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆ์ปท์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. const returnMe = name => name

์ด๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  1. const returnMe = name => {
  2.     return name;
  3. }

Exports & Imports

React ํ”„๋กœ์ ํŠธ์—์„œ (๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ๋กœ ๋ชจ๋“  ์ตœ์‹  JavaScript์—์„œ), ๋ชจ๋“ˆ์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๋“ค์— ์ฝ”๋“œ๋ฅผ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ file/ ๋ชจ๋“ˆ์˜ ๋ชฉ์ ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ณ  ๊ด€๋ฆฌ๊ฐ€ ์šฉ์ดํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ํŒŒ์ผ์˜ ๊ธฐ๋Šฅ์— ๊ณ„์† ์•ก์„ธ์Šคํ•˜๋ ค๋ฉด export  (availableํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด) ๋ฐ import ์—‘์„ธ์Šค๋ฅผ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•ด) statements๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋‘ ๊ฐ€์ง€ ์œ ํ˜•์˜ export๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: default (unnamed)์™€ named ์ž…๋‹ˆ๋‹ค.

default => export default ...; 

named => export const someData = ...; 

default exports๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด import ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import someNameOfYourChoice from './path/to/file.js'; 

๋†€๋ž๊ฒŒ๋„, someNameOfYourChoice  ์ „์ ์œผ๋กœ ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ๋‹ฌ๋ ค ์žˆ์Šต๋‹ˆ๋‹ค.

Named exports๋Š” ์ด๋ฆ„์œผ๋กœ import๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

import { someData } from './path/to/file.js'; 

ํŒŒ์ผ ํ•˜๋‚˜๋Š” ์˜ค์ง ํ•˜๋‚˜์˜ default์™€ ๋ฌดํ•œํ•œ named exports๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ default๋ฅผ ๊ฐ™์€ ํŒŒ์ผ ๋‚ด์—์„œ named exports์™€ ๋ฏน์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

named exports๋ฅผ importํ•  ๋•Œ, ๋‹ค์Œ ๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด ํ•œ ๋ฒˆ์— ๋ชจ๋“  named exports๋ฅผ importํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import * as upToYou from './path/to/file.js'; 

upToYou ๋Š” ๋ชจ๋“  exported ๋ณ€์ˆ˜/ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์— ๋ชจ์๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, export const someData = ...  (/path/to/file.js ) ์ด์™€ ๊ฐ™์ด upToYou ์— ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: upToYou.someData .

Classes

Classes๋Š” constructor ํ•จ์ˆ˜์™€ prototypes๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์— blueprints๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ:

  1. class Person {
  2.     constructor () {
  3.         this.name = 'Max';
  4.     }
  5. }
  6.  
  7. const person = new Person();
  8. console.log(person.name); // prints 'Max'

์œ„์˜ ์˜ˆ์‹œ์—์„œ, class๋ฟ ๋งŒ ์•„๋‹ˆ๋ผ ํ•ด๋‹น class์˜ property (=> name) ์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ตฌ๋ฌธ์€, property๋ฅผ ์ •์˜ํ•˜๋Š” "๊ตฌ์‹" ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ตœ์‹  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” (์ด ์ฝ”์Šค์—์„œ ์‚ฌ์šฉ๋œ ๊ฒƒ์ฒ˜๋Ÿผ), ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณด๋‹ค ํŽธ๋ฆฌํ•œ ์ •์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด class property๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค:

  1. class Person {
  2.     name = 'Max';
  3. }
  4.  
  5. const person = new Person();
  6. console.log(person.name); // prints 'Max'

๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ง์ด์ฃ :

  1. class Person {
  2.     name = 'Max';
  3.     printMyName () {
  4.         console.log(this.name); // this is required to refer to the class!
  5.     }
  6. }
  7.  
  8. const person = new Person();
  9. person.printMyName();

ํ˜น์€ ์ด์™€ ๊ฐ™์ด ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. class Person {
  2.     name = 'Max';
  3.     printMyName = () => {
  4.         console.log(this.name);
  5.     }
  6. }
  7.  
  8. const person = new Person();
  9. person.printMyName();

๋‘ ๋ฒˆ์งธ ์ ‘๊ทผ ๋ฐฉ์‹์€ all arrow function๊ณผ ๊ฐ™์€ ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค: thisํ‚ค์›Œ๋“œ๊ฐ€ reference๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

class ์‚ฌ์šฉ์‹œ inheritance๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. class Human {
  2.     species = 'human';
  3. }
  4.  
  5. class Person extends Human {
  6.     name = 'Max';
  7.     printMyName = () => {
  8.         console.log(this.name);
  9.     }
  10. }
  11.  
  12. const person = new Person();
  13. person.printMyName();
  14. console.log(person.species); // prints 'human'

Spread & Rest Operator

Spread ์™€ rest operator๋Š” ์‚ฌ์‹ค ๊ฐ™์€ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: ... 

๋งž์Šต๋‹ˆ๋‹ค, ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค - ์  ์„ธ๊ฐœ์ฃ . ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•ด spread๋กœ ์‚ฌ์šฉํ• ์ง€ rest operator๋กœ ์‚ฌ์šฉํ• ์ง€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

Spread Operator ์‚ฌ์šฉํ•˜๊ธฐ:

Spread operator๋Š” ๋ฐฐ์—ด์—์„œ ์š”์†Œ๋“ค์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ (=> ๋ฐฐ์—ด์„ ์š”์†Œ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„ํ•ด) ๊ฐ์ฒด์—์„œ ์†์„ฑ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

๋‘ ๊ฐ€์ง€ ์˜ˆ์‹œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. const oldArray = [1, 2, 3];
  2. const newArray = [...oldArray, 4, 5]; // This now is [1, 2, 3, 4, 5];

๊ฐ์ฒด์— spread operator๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค:

  1. const oldObject = {
  2.     name: 'Max'
  3. };
  4. const newObject = {
  5.     ...oldObject,
  6.     age: 28
  7. };

๊ทธ๋Ÿฌ๋ฉด newObject๋Š” ๋‹ค์Œ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

  1. {
  2.     name: 'Max',
  3.     age: 28
  4. }

sperad operator๋Š” ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•˜๋Š”๋ฐ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค  (primitives๊ฐ€ ์•„๋‹Œ) reference ์œ ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์—, ์•ˆ์ •์ ์œผ๋กœ ๋ณต์‚ฌ๋ฅผ ํ•˜๋Š”๊ฒŒ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋ณต์‚ฌ๋œ ์›๋ณธ์— future mutation ๋ฐœ์ƒ ๋ฐฉ์ง€). Spread operator๋กœ, ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์˜ ๋ณต์‚ฌ๋ณธ (shallow!)์„ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Destructuring

Destructuring์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ๊ฐ’์— ์‰ฝ๊ฒŒ ์—‘์„ธ์Šคํ•  ์ˆ˜ ์žˆ๊ณ  ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•œ ๋ฐฐ์—ด์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค:

  1. const array = [1, 2, 3];
  2. const [a, b] = array;
  3. console.log(a); // prints 1
  4. console.log(b); // prints 2
  5. console.log(array); // prints [1, 2, 3]

๋‹ค์Œ์€ ๊ฐ์ฒด์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค:

  1. const myObj = {
  2.     name: 'Max',
  3.     age: 28
  4. }
  5. const {name} = myObj;
  6. console.log(name); // prints 'Max'
  7. console.log(age); // prints undefined
  8. console.log(myObj); // prints {name: 'Max', age: 28}

Destructuring์€ ์ธ์ž๋ฅผ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ์ž‘์—…ํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ˆ์‹œ๋ฅผ ๋ณด์‹œ์ฃ :

  1. const printName = (personObj) => {
  2.     console.log(personObj.name);
  3. }
  4. printName({name: 'Max', age: 28}); // prints 'Max'

์—ฌ๊ธฐ์„œ, ํ•จ์ˆ˜๋‚ด name๋งŒ์„ printํ•˜๊ณ  ์‹ถ์ง€๋งŒ ํ•จ์ˆ˜์— ์™„์ „ํ•œ person ๊ฐ์ฒด๋ฅผ ๋ณด๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ ์ด๊ฒƒ์€ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์ง€๋งŒ personObj.name์„ ์ด ํ•จ์ˆ˜๋‚ด์—์„œ ํ˜ธ์ถœํ•ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ destructuring์œผ๋กœ ์••์ถ•์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  1. const printName = ({name}) => {
  2.     console.log(name);
  3. }
  4. printName({name: 'Max', age: 28}); // prints 'Max')

์œ„์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. Destructuring์„ ํ†ตํ•ด, name property๋ฅผ ๊ฐ€์ ธ์™€ name ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ณ€์ˆ˜/์ธ์ˆ˜์— ์ €์žฅํ•˜๊ณ  ํ•จ์ˆ˜ ๋ณธ๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

JS Array functions

์ฐจ์„ธ๋Œ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์•„๋‹ˆ์ง€๋งŒ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ array ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: map() , filter() , reduce().

๋งŽ์€ React ๊ฐœ๋…์ด (๋ถˆ๋ณ€์˜ ๋ฐฉ์‹์œผ๋กœ) ๋ฐฐ์—ด ์ž‘์—…์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ œ๊ฐ€ ๊ทธ๊ฒƒ๋“ค์„ ๊ฝค ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ณด๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ ํŽ˜์ด์ง€๋Š” ์–ด๋ ˆ์ด ํ”„๋กœํ† ํƒ€์ž…์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ข‹์€ ๊ฐœ์š”๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ•„์š”์— ๋”ฐ๋ผ ์ด๋ฅผ ํด๋ฆญํ•˜๊ณ  ์ง€์‹์„ ๋ฆฌํ”„๋ ˆ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

์ด ์ฝ”์Šค์—์„œ ํŠนํžˆ ์ค‘์š”ํ•œ ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

 

 

Sources

https://academind.com/

 

Academind

Academind offers hundreds of coding tutorials and premium programming video courses. We have a 30 day refund period, more than 1.5mn active students and more than 500.000 5 star rating.

academind.com