๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • Spread Operator
  • containing array
  • true copy 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•
  • proto : NodeList => Array
  • spread operator + string
  • ์ œ์™ธ ์‹œํ‚ค๊ธฐ
  • call, apply ๋Œ€์‹  ์‚ฌ์šฉํ•˜๊ธฐ
  • Spreading into a function
  • Rest Param
  • + spread operator

Was this helpful?

  1. ecma-script2015

spread-operator-rest-param

Spread Operator

์˜ˆ์‹œ๋กœ ํ™•์ธํ•˜์Ÿˆ 'ใ…'/

containing array

const featured = ['Deep dish', 'Peperoni', 'Hawaiian'];
const specialty = ['Meatzza', 'Spicy Nama', 'Margherita'];
const pizas = featured.concat(specialty);

console.log(pizas); // ["Deep dish", "Peperoni", "Hawaiian", "Meatzza", "Spicy Nama", "Margherita"]

๋งŒ์•ฝ ์ค‘๊ฐ„์— ๋˜ ๋‹ค๋ฅธ ์ถ”๊ฐ€๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด?

const featured = ['Deep dish', 'Peperoni', 'Hawaiian'];
const specialty = ['Meatzza', 'Spicy Nama', 'Margherita'];

let pizzas = [];
pizzas = pizzas.concat(featured);
pizzas.push('veg');
pizzas = pizzas.concat(specialty);
console.log(pizzas); // ["Deep dish", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita"]

ํ•˜์ง€๋งŒ spread operator๋ฅผ ์“ด๋‹ค๋ฉด!

const featured = ['Deep dish', 'Peperoni', 'Hawaiian'];
const specialty = ['Meatzza', 'Spicy Nama', 'Margherita'];

let pizzas = [...featured, 'veg', ...specialty];
console.log(pizzas); //["Deep dish", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita"]

:open_mouth: ์—„์ฒญ๋‚˜๊ฒŒ ๊น”๋”ํ•ด์กŒ๋‹ค! :open_mouth:

Array copy

const featured = ['Deep dish', 'Peperoni', 'Hawaiian'];
const specialty = ['Meatzza', 'Spicy Nama', 'Margherita'];

let pizzas = [...featured, 'veg', ...specialty];
let fridayPizzas = pizzas; 

fridayPizzas[0] = 'Canada Pie';

console.log(pizzas); // ["Canada Pie", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita"]

console.log(fridayPizzas); //["Canada Pie", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita"]

Array๋Š” ์ฐธ์กฐ๊ฐ’์ด ๋ณต์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กญ๊ฒŒ ๋ณต์‚ฌํ•œ fridayPizzas ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด pizzas๋„ ๊ฐ™์ด ์ˆ˜์ •๋œ๋‹ค.

true copy 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•

const featured = ['Deep dish', 'Peperoni', 'Hawaiian'];
const specialty = ['Meatzza', 'Spicy Nama', 'Margherita'];

let pizzas = [...featured, 'veg', ...specialty];

// ๋ฐฉ๋ฒ• 1 
const copyPizzas1 = pizzas.slice(); 

// ๋ฐฉ๋ฒ• 2 
const copyPizzas2 = [].concat(pizzas); 

// ๋ฐฉ๋ฒ• 3
const copyPizzas3 = [...pizzas];

// ๋ฐฉ๋ฒ• 4
const copyPizzas4 = Array.from(pizzas);

copyPizzas1.push('cheeze1');
copyPizzas2.push('cheeze2');
copyPizzas3.push('cheeze3');
copyPizzas4.push('cheeze4');

console.log(pizzas); // ["Deep dish", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita"]

console.log(copyPizzas1); //["Deep dish", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita", "cheeze1"]

console.log(copyPizzas2); // ["Deep dish", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita", "cheeze2"]

console.log(copyPizzas3); // ["Deep dish", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita", "cheeze3"]

console.log(copyPizzas4); // ["Deep dish", "Peperoni", "Hawaiian", "veg", "Meatzza", "Spicy Nama", "Margherita", "cheeze4"]

์ด 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, reference ํ•œ๊ฒŒ ์•„๋‹ˆ๋ผ ์›์†Œ๋“ค์„ ๋ณต์‚ฌ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€, ์ˆ˜์ •ํ•˜์—ฌ๋„ pizzas๊ฐ€ ์ˆ˜์ •๋˜์ง€ ์•Š๋Š”๋‹ค.

proto : NodeList => Array

const people = document.querySelectorAll('.people p'); // NodeList

const people1 = Array.from(document.querySelectorAll('.people p')); // Array

const people2 = [...document.querySelectorAll('.people p')]; // Array

spread operator๋ฅผ ํ†ตํ•ด NodeList ๋„ Array๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

spread operator + string

console.log([...'wes']) // ['w', 'e', 's'];

์ œ์™ธ ์‹œํ‚ค๊ธฐ

const comments = [
    { id: 209384, text: 'I love your dog!' },
    { id: 523423, text: 'Cuuute!' },
    { id: 632429, text: 'You are so dumb' },
    { id: 192834, text: 'Nice work on this wes!' },
]

// id๊ฐ€ 632429์ธ๊ฒƒ์„ ์ œ์™ธ์‹œํ‚ค์ž
const id = 632429;
const commentIndex = comments.findIndex(comment => comment.id === id);

// ์ „๊ฐœ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด 

const newComments1 = [comments.slice(0, commentIndex), comments.slice(commentIndex + 1)];

// ๊ฒฐ๊ณผ๋Š” [ [ { id: 209384, text: 'I love your dog!' }, { id: 523423, text: 'Cuuute!' } ] , [ { id: 192834, text: 'Nice work on this wes!' } ]]

// ์›์†Œ๊ฐ€ 2๊ฐœ์ธ ๋ฐฐ์—ด๊ณผ + ์›์†Œ๊ฐ€ 1๊ฐœ์ธ ๋ฐฐ์—ด์ด ํ•ฉ์ณ์ง„ ๋ฐฐ์—ด์ด ๋œ๋‹ค.

// ์ „๊ฐœ ์—ฐ์‚ฐ์ž spread operator๋ฅผ ์‚ฌ์šฉํ•˜์ž

const newComments2 = [...comments.slice(0, commentIndex), ...comments.slice(commentIndex + 1)];

// ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์›ํ•˜๋Š” ๊ฐ’๋„ ์–ป์„ ์ˆ˜ ์žˆ๊ณ  react์—๋„ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.
this.setState({ comments: newComments });

call, apply ๋Œ€์‹  ์‚ฌ์šฉํ•˜๊ธฐ

const inventors = ['Einstein', 'Newton', 'Galileo'];
const newInventors = ['Musk', 'Jobs'];

// ๊ธฐ์กด ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๊ธฐ
inventors.push.apply(inventors, newInventors);
console.log(inventors);

apply์˜ ๋‘๋ฒˆ์งธ ์ธ์ž๊ฐ€ ์ดํ•ด๊ฐ€ ์•ˆ๋˜์–ด ๋” ์ฐพ์•„ ๋ณด์•˜๋‹ค.

apply๋Š” argsArray ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์œ„ํ•œ arguments๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

fun.apply(thisArg, [argsArray])

apply์˜ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” array๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด ์ ์„ ์ด์šฉํ•ด ๋‘๋ฒˆ์งธ ์ธ์ž์— ๋ฐฐ์—ด์„ ๋ณด๋‚ด๊ณ  push๊ฐ€ ์‹คํ–‰๋  ๋•Œ์—๋Š” ๋ฐฐ์—ด์ด ํ’€๋ฆฐ๋‹ค. ์ด์ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๊ธฐ์ €๊ธฐ์— ์“ฐ์ธ๋‹ค.

MDN ์˜ˆ์‹œ - ๋‚ด์žฅํ•จ์ˆ˜ ์‚ฌ์šฉ

var numbers = [5, 6, 2, 3, 7];
var max = Math.max.apply(null, numbers);

console.log(max);
// expected output: 7

๋‹ค์‹œ ๋ณด์ž

inventors.push.apply(inventors, newInventors);

this๋ถ€๋ถ„์— null๋ผ๊ณ  ์ž‘์„ฑํ•˜๋ฉด ์•ˆ๋œ๋‹ค. ์ƒ์„ฑ์žํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์ง์ ‘์ ์ธ ๋„ค์ž„์„ ๊ฐ€์ง„ inventors์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•ด์„œ ์•„๋‹๊นŒ? ๊ทธ ๋‹ค์Œ์€ ์ธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— array๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

inventors.push(...newInventors);

๋!

์œ„์˜ MDN์˜ˆ์‹œ๋„ ์‰ฝ๊ฒŒ ์ •๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค.

var numbers = [5, 6, 2, 3, 7];
var max = Math.max(...numbers);

์ •๋ง ๋Œ€๋‹จํ•˜๋‹ค.. :open_mouth:

Spreading into a function

const name = ['pikachu', 'spinda'];

function sayHi(first, second) {
    console.log(`Hey ${first} ${second}`);
}

sayHi(...name);

Rest Param

function convertCurrency(rate, tax, tip, ...amounts) {
    console.log(rate, tax, tip, amounts);
    return amounts.map(amount => amount * rate);
}

const amounts = convertCurrency(1.45, 10, 23, 52, 1, 56);
console.log(amounts); // 1.45 10 23 [52, 1, 56]

๋งค๊ฐœ๋ณ€์ˆ˜ ์ž๋ฆฌ์— ์“ด๋‹ค. ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋œปํ•œ๋‹ค.

+ spread operator

const runner = ['pikachu', 123, 5.5, 5, 3, 6, 35];
const [name, id, ...runs] = runner;

console.log(name, id, runs); //pikachu 123 [5.5, 5, 3, 6, 35
Previousdefault-parameter-template-literals-arrow-functionsNextlet-const-rest-parameter-spread-operator-destructuring

Last updated 5 years ago

Was this helpful?