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
์ด ๋ชจ๋์์, ์ ๋ ๋ช๋ช ํต์ฌ ์ฐจ์ธ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ๋ฅ๋ค์ ๋ํ ๊ฐ๋ตํ ์๊ฐ๋ฅผ ํด ๋๋ ธ์ต๋๋ค. ๋ฌผ๋ก ์ด ๊ณผ์ ์์ ์ฌ๋ฌ๋ถ๋ค์ด ์์ฃผ ๋ณด์๊ฒ ๋ ๊ฒ๋ค์ ์ด์ ์ ๋ง์ถ์์ฃ . ์ฌ๊ธฐ ๊ฐ๋ตํ ์์ฝ์ด ์์ต๋๋ค!
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 ๊ตฌ๋ฌธ์ ๋ฏ์ค๊ฒ ๋ณด์ผ ์ ์์ผ๋ ์ฌ์ค ๊ฐ๋จํฉ๋๋ค.
- function callMe(name) {
- console.log(name);
- }
๋ํ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์๋ ์์ต๋๋ค:
- const callMe = function(name) {
- console.log(name);
- }
์ด๋ ๊ฒ ๋ฉ๋๋ค:
- const callMe = (name) => {
- console.log(name);
- }
์ค์:
arguments๊ฐ ์๋ ๊ฒฝ์ฐ, ํจ์ ์ ์ธ์ ๋น ๊ดํธ๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค:
- const callMe = () => {
- console.log('Max!');
- }
์ ํํ ํ๋์ argument๊ฐ ์๋ ๊ฒฝ์ฐ, ๊ดํธ๋ฅผ ์๋ตํ ์ ์์ต๋๋ค:
- const callMe = name => {
- console.log(name);
- }
value๋ฅผ returnํ ๋, ๋ค์๊ณผ ๊ฐ์ ์์ปท์ ์ฌ์ฉํ ์ ์์ต๋๋ค:
- const returnMe = name => name
์ด๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
- const returnMe = name => {
- return name;
- }
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๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
์์:
- class Person {
- constructor () {
- this.name = 'Max';
- }
- }
- const person = new Person();
- console.log(person.name); // prints 'Max'
์์ ์์์์, class๋ฟ ๋ง ์๋๋ผ ํด๋น class์ property (=> name) ์ด ์ ์๋ฉ๋๋ค. ํด๋น ๊ตฌ๋ฌธ์, property๋ฅผ ์ ์ํ๋ "๊ตฌ์" ๊ตฌ๋ฌธ์ ๋๋ค. ์ต์ ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก์ ํธ์์๋ (์ด ์ฝ์ค์์ ์ฌ์ฉ๋ ๊ฒ์ฒ๋ผ), ๋ค์๊ณผ ๊ฐ์ ๋ณด๋ค ํธ๋ฆฌํ ์ ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด class property๋ฅผ ์ ์ํฉ๋๋ค:
- class Person {
- name = 'Max';
- }
- const person = new Person();
- console.log(person.name); // prints 'Max'
๋ฉ์๋๋ฅผ ์ ์ํ ์๋ ์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ๋ง์ด์ฃ :
- class Person {
- name = 'Max';
- printMyName () {
- console.log(this.name); // this is required to refer to the class!
- }
- }
- const person = new Person();
- person.printMyName();
ํน์ ์ด์ ๊ฐ์ด ํ ์๋ ์์ต๋๋ค:
- class Person {
- name = 'Max';
- printMyName = () => {
- console.log(this.name);
- }
- }
- const person = new Person();
- person.printMyName();
๋ ๋ฒ์งธ ์ ๊ทผ ๋ฐฉ์์ all arrow function๊ณผ ๊ฐ์ ์ด์ ์ด ์์ต๋๋ค: thisํค์๋๊ฐ reference๋ฅผ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค.
class ์ฌ์ฉ์ inheritance๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
- class Human {
- species = 'human';
- }
- class Person extends Human {
- name = 'Max';
- printMyName = () => {
- console.log(this.name);
- }
- }
- const person = new Person();
- person.printMyName();
- console.log(person.species); // prints 'human'
Spread & Rest Operator
Spread ์ rest operator๋ ์ฌ์ค ๊ฐ์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํฉ๋๋ค: ...
๋ง์ต๋๋ค, ์ฐ์ฐ์์ ๋๋ค - ์ ์ธ๊ฐ์ฃ . ์ด๊ฒ์ ์ฌ์ฉํด spread๋ก ์ฌ์ฉํ ์ง rest operator๋ก ์ฌ์ฉํ ์ง ๊ฒฐ์ ํฉ๋๋ค.
Spread Operator ์ฌ์ฉํ๊ธฐ:
Spread operator๋ ๋ฐฐ์ด์์ ์์๋ค์ ๊ฐ์ ธ์ค๊ฑฐ๋ (=> ๋ฐฐ์ด์ ์์๋ค์ ๋ฆฌ์คํธ๋ก ๋ถํด) ๊ฐ์ฒด์์ ์์ฑ์ ๊ฐ์ ธ์ต๋๋ค.
๋ ๊ฐ์ง ์์๊ฐ ์์ต๋๋ค:
- const oldArray = [1, 2, 3];
- const newArray = [...oldArray, 4, 5]; // This now is [1, 2, 3, 4, 5];
๊ฐ์ฒด์ spread operator๋ฅผ ์ฌ์ฉํ ์์์ ๋๋ค:
- const oldObject = {
- name: 'Max'
- };
- const newObject = {
- ...oldObject,
- age: 28
- };
๊ทธ๋ฌ๋ฉด newObject๋ ๋ค์์ด ๋ ๊ฒ์ ๋๋ค.
- {
- name: 'Max',
- age: 28
- }
sperad operator๋ ๋ฐฐ์ด๊ณผ ๊ฐ์ฒด๋ฅผ ๋ณต์ ํ๋๋ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ๋ ๋ค (primitives๊ฐ ์๋) reference ์ ํ์ด๊ธฐ ๋๋ฌธ์, ์์ ์ ์ผ๋ก ๋ณต์ฌ๋ฅผ ํ๋๊ฒ ์ด๋ ค์ธ ์ ์์ต๋๋ค. (๋ณต์ฌ๋ ์๋ณธ์ future mutation ๋ฐ์ ๋ฐฉ์ง). Spread operator๋ก, ๊ฐ์ฒด๋ ๋ฐฐ์ด์ ๋ณต์ฌ๋ณธ (shallow!)์ ์ฝ๊ฒ ์ป์ ์ ์์ต๋๋ค.
Destructuring
Destructuring์ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด์ ๊ฐ์ ์ฝ๊ฒ ์์ธ์คํ ์ ์๊ณ ๋ณ์์ ํ ๋นํ ์ ์์ต๋๋ค.
ํ ๋ฐฐ์ด์ ์์์ ๋๋ค:
- const array = [1, 2, 3];
- const [a, b] = array;
- console.log(a); // prints 1
- console.log(b); // prints 2
- console.log(array); // prints [1, 2, 3]
๋ค์์ ๊ฐ์ฒด์ ์์์ ๋๋ค:
- const myObj = {
- name: 'Max',
- age: 28
- }
- const {name} = myObj;
- console.log(name); // prints 'Max'
- console.log(age); // prints undefined
- console.log(myObj); // prints {name: 'Max', age: 28}
Destructuring์ ์ธ์๋ฅผ ๊ฐ์ง ํจ์๋ฅผ ์์ ํ ๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ์ด ์์๋ฅผ ๋ณด์์ฃ :
- const printName = (personObj) => {
- console.log(personObj.name);
- }
- printName({name: 'Max', age: 28}); // prints 'Max'
์ฌ๊ธฐ์, ํจ์๋ด name๋ง์ printํ๊ณ ์ถ์ง๋ง ํจ์์ ์์ ํ person ๊ฐ์ฒด๋ฅผ ๋ณด๋ด๊ณ ์์ต๋๋ค. ๋น์ฐํ ์ด๊ฒ์ ๋ฌธ์ ๊ฐ ๋์ง ์์ง๋ง personObj.name์ ์ด ํจ์๋ด์์ ํธ์ถํด์ผ๋ง ํฉ๋๋ค. ์ด ์ฝ๋๋ฅผ destructuring์ผ๋ก ์์ถ์์ผ ๋ณด๊ฒ ์ต๋๋ค.
- const printName = ({name}) => {
- console.log(name);
- }
- printName({name: 'Max', age: 28}); // prints 'Max')
์์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ง๋ง ์ฝ๋๊ฐ ์ค์์ต๋๋ค. Destructuring์ ํตํด, name property๋ฅผ ๊ฐ์ ธ์ name ์ด๋ผ๋ ์ด๋ฆ์ ๋ณ์/์ธ์์ ์ ์ฅํ๊ณ ํจ์ ๋ณธ๋ฌธ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฐจ์ธ๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ ์๋์ง๋ง ์ค์ํฉ๋๋ค. ๋ค์๊ณผ ๊ฐ์ ์๋ฐ์คํฌ๋ฆฝํธ array ํจ์๊ฐ ์์ต๋๋ค: map() , filter() , reduce().
๋ง์ React ๊ฐ๋ ์ด (๋ถ๋ณ์ ๋ฐฉ์์ผ๋ก) ๋ฐฐ์ด ์์ ์ ์์กดํ๊ธฐ ๋๋ฌธ์ ์ ๊ฐ ๊ทธ๊ฒ๋ค์ ๊ฝค ๋ง์ด ์ฌ์ฉํ๋ ๊ฒ์ ๋ณด๊ฒ ๋ ๊ฒ์ ๋๋ค.
๋ค์ ํ์ด์ง๋ ์ด๋ ์ด ํ๋กํ ํ์ ์์ ์ฌ์ฉํ ์ ์๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ๋ํ ์ข์ ๊ฐ์๋ฅผ ์ ๊ณตํฉ๋๋ค. ํ์์ ๋ฐ๋ผ ์ด๋ฅผ ํด๋ฆญํ๊ณ ์ง์์ ๋ฆฌํ๋ ์ํ ์ ์์ต๋๋ค. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
์ด ์ฝ์ค์์ ํนํ ์ค์ํ ์ฌํญ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
- map() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
- find() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
- findIndex() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
- filter() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
- reduce() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=b
- concat() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat?v=b
- slice() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
- splice() => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Sources
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