다음과 같이 인수(name)을 받아서 출력하는 함수가 있을 때, 인수는 꼭 하나만 들어오지 않을 수도 있다.
function showName(name){
console.log(name);
}
showName('성원'); // '성원'
showName('성원', '달봉'); // '성원'
showName(); // undefined
function showName(name){
console.log(arguments.length);
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments)
}
showName('성원', '달봉');
// 2
// 성원
// 달봉
/* Arguments(2) ['성원', '달봉', callee: ƒ, Symbol(Symbol.iterator): ƒ]
0: "성원"
1: "달봉"
*/
function showName(...arrNm){
console.log(arrNm);
}
showName(); // []
showName('성원'); // ['성원']
showName('성원', '달봉'); // ['성원', '달봉']
// reduce
function addNum(...numbers){
let result = numbers.reduce((pre, cur) => {
return pre + cur;
}, 0);
console.log(result);
}
addNum(1, 2, 3);
// forEach
function addNum2(...numbers){
let result = 0;
numbers.forEach((num) => {
result += num;
});
console.log(result);
}
addNum2(1, 2, 3, 4);
function User(name, age, ...skills){
this.name = name;
this.age = age;
this.skills = skills;
}
const user1 = new User('성원', 29, 'html', 'css', 'js');
const user2 = new User('달봉', 31, 'python');
const user3 = new User('쿠몬', 33, 'veu', 'C');
console.log(user1);
console.log(user2);
console.log(user3);
/*
User {name: '성원', age: 29, skills: Array(3)}
age: 29
name: "성원"
skills: (3) ['html', 'css', 'js']
*/
/*
User {name: '달봉', age: 31, skills: Array(1)}
age: 31
name: "달봉"
skills: ['python']
*/
/*
User {name: '쿠몬', age: 33, skills: Array(2)}
age: 33
name: "쿠몬"
skills: (2) ['veu', 'C']
*/
let arr1 = [1, 2, 3];
let arr2 = [10, 20, 30];
console.log(...arr1);
console.log(...arr2);
let result = [0, ...arr1, '...', ...arr2, '...', 100, 200, 300];
console.log(result);
// 1 2 3
// 10 20 30
// [0,1,2,3,"...",10,20,30,"...",100,200,300]
let user = {name: 'Mike'}
let mike = {...user, age : 30}
console.log(mike);
// {name: 'Mike', age: 30}
깊은복사
가 가능let arr1 = [1, 2, 3];
let arr2 = [...arr1];
let user1 = {name: 'Mike', age: 30};
let user2 = {...user1};
arr1[0] = 100;
user1.name = 'Jane';
console.log(arr1);
console.log(arr2);
console.log(user1);
console.log(user2);
// [100, 2, 3]
// [1, 2, 3]
// {name: 'Jane', age: 30}
// {name: 'Mike', age: 30}
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
// 1. 배열메소드 사용
arr2.forEach((num) => arr1.unshift(num));
console.log(arr1); // [6,5,4,1,2,3]
// 1-1
arr2.reverse().forEach((num) => arr1.unshift(num));
console.log(arr1); // [4,5,6,1,2,3]
// 2. 전개 구문 사용
let result = [...arr2, ...arr1]
console.log(result); // [4,5,6,1,2,3]
let user = { name: 'Mike' };
let info = { age: 30 };
let fe = ['JS', 'React'];
let lang = ['Ko', 'Eng'];
// assign 사용
user = Object.assign({}, user, info, {
skills: [],
});
fe.forEach((item) => {
user.skills.push(item)
});
lang.forEach((item) => {
user.skills.push(item)
});
console.log(user);
/*
age: 30
name: "Mike"
skills: (4) ['JS', 'React', 'Ko', 'Eng']
*/
let user = { name: 'Mike' };
let info = { age: 30 };
let fe = ['JS', 'React'];
let lang = ['Ko', 'Eng'];
// 전개구문 사용
user = {
...user,
...info,
skills: [...fe, ...lang],
}
console.log(user);
/*
age: 30
name: "Mike"
skills: (4) ['JS', 'React', 'Ko', 'Eng']
*/