const slime = {
name: '슬라임',
}
const cuteSlime = {
name: '슬라임',
attribute: 'cute',
}
const purpleCuteSlime = {
name: '슬라임',
attribute: 'cute',
color: 'purple',
}
console.log(slime)
console.log(cuteSlime)
console.log(purpleCuteSlime)
const slime = {
name: '슬라임',
}
const cuteSlime = {
...slime,
attribute: 'cute',
}
const purpleCuteSlime = {
...cuteSlime,
color: 'purple',
}
console.log(slime)
console.log(cuteSlime)
console.log(purpleCuteSlime)
const animals = ['개', '고양이', '참새']
const anotherAnimals = [...animals, '비둘기']
console.log(animals)
console.log(anotherAnimals)
const numbers = [1, 2, 3, 4, 5]
const spreadNumbers = [...numbers, 1000, ...numbers]
console.log(spreadNumbers) //[1, 2, 3, 4, 5, 1000, 1, 2, 3, 4, 5]
const numbers = [0, 1, 2, 3, 4, 5, 6]
const [one, ...rest] = numbers
console.log(one)
console.log(rest)
function sum(a, b, c, d, e, f, g) {
let sum = 0
if (a) sum += a
if (b) sum += b
if (c) sum += c
if (d) sum += d
if (e) sum += e
if (f) sum += f
if (g) sum += g
return sum
}
const result = sum(1, 2, 3, 4, 5, 6)
console.log(result) //21
function sum(...rest) {
return rest
}
const result = sum(1, 2, 3, 4, 5, 6)
console.log(result) // [1, 2, 3, 4, 5, 6]
function sum(...rest) {
return rest.reduce((acc, current) => acc + current, 0)
}
const result = sum(1, 2, 3, 4, 5, 6)
console.log(result)
function sum(...rest) {
return rest.reduce((accumulator, current) => accumulator + current, 0)
}
const result = sum(1, 2, 3, 4, 5, 6)
console.log(result)
function sum(...rest) {
return rest.reduce((accumulator, current) => {
console.log({ accumulator, current })
return accumulator + current
}, 0)
}
const result = sum(1, 2, 3, 4, 5, 6)
console.log(result)
const myFunction(a) { // a: 파라미터
console.log(a) // a: 인자
}
myFunction('hello world') // 'hello world'는 인자
function sum(...rest) {
return rest.reduce((acc, current) => acc + current, 0)
}
const numbers = [1, 2, 3, 4, 5, 6]
const result = sum(
numbers[0],
numbers[1],
numbers[2],
numbers[3],
numbers[4],
numbers[5]
)
console.log(result)
function sum(...rest) {
return rest.reduce((acc, current) => acc + current, 0)
}
const numbers = [1, 2, 3, 4, 5, 6]
const result = sum(...numbers)
console.log(result)
해당 내용은 다음 자료를 참고했습니다.
https://learnjs.vlpt.us/useful/07-spread-and-rest.html