코딩 공부/코드스테이츠 TIL

Code States 13일차

fullfish 2021. 10. 26. 09:54

오늘의 키포인트

구조분해(Spread, Rest)


나의 이해


Spread : 배열을 배열아닌 요소로 펼침
Rest : 매개변수로 쓰을 떄, 배열아닌걸 배열형태로 받음


문법 및 중요

Spread 

function sum(x, y, z) {
  return x + y + z;
}
const numbers = [1, 2, 3];
sum(...numbers) // 6

-----------------------

const name= {

  name: '만선'

};

 

const attribute{

  name: '만선',

  attribute: '좋음'

};

 

const color= {

  name: '살색',

  attribute: '좋음',

  color: '살색'

};

 

console.log(name);

console.log(attribute);

console.log(color);

이런것을

const name= {

  name: '만선'
};
const attribute= {
  ...slime,
  attribute: '좋음'
};
 const color = {
  ...attribute,
  color: '살색'
};
 console.log(name);
console.log(attribute);
console.log(color);
이렇게 할 수 있다

 

let value= [1,2,3]
let value = Math.max(value); //
이러면 NaN나옴 Math.max(…value); 해야함
펼쳐서 넣어주는것임 즉, 배열같은것을 배열아니고 요소로
a=[1,2]
이면 b=…a하면 안되고 b=[…a]해야함

 

Rest

function sum(...num) {
  return num.reduce((previous, current) => {
    return previous + current;
  });
}
sum(1,2,3) // 6
sum(1,2,3,4) // 10

 

구조분해

const [a, b, ...rest] = [10, 20, 30, 40]; // a : 10, b : 20, rest : [30, 40]

const {a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40} // a : 10, b : 20, rest : {c : 30, d : 40}

구조 분해 할당시 선언자 같이 써야함

 

const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
const returnedTarget = Object.assign(target, source);

target은 {a : 1, b : 3, c : 4}, source는 {b : 3, c : 4}

 

function abc() {

      return arguments;

    }

 

이런식으로하면 매개변수 상관없이 arguments에 다 담아버림 알규먼트는 무조건 오브젝트

새로 안것

테스트케이스

expect(조건).to.be.true;

expect(함수든 뭐든).to.equal(같으면 true);

TDD : 테스트 주도 개발 // 테스트 케이스 넣어서 개발하는거


호이스팅 : 함수 안에 선언된 모든 변수들을 끌어올려서 해당 함수의 유효 범위(스코프) 최상단에 선언하는 것

 

objectlength 사용 불가

 

복사의 종류
1.
단순 객체 복사 (주소 참조)
  let a = [1, 2, 3];
  let b = a;


2.
얕은 복사(shallow copy) : 다른 주소이지만, 참조 요소는 복사되지 않음 ex) Object.assign
  const target = {a : 1, b : 2};
  const source = {b : 4, c : 5};
  const returnedTarget = Object.assign{targer, source}


3.
깊은 복사(deep copy) : 전부 온전하게 다른 주소
 
방법1. JSON.stringify : 객체를 스트링 포멧으로 변환
 
방법2. 반복문 활용 복사
 
방법3. lodash 라이브러리의 cloneDeep() 메소드 활용

 

arguments 객체는 배열이 아니기 때문에, 배열 메소드인 push 사용할 없습니다. 점이 오랫동안 자바스크립트의 불편한 점으로 남아있어, ES6에서는 rest parameter 사용할 있게 되었습니다.

 

'코딩 공부 > 코드스테이츠 TIL' 카테고리의 다른 글

Code States 16일차  (0) 2021.10.28
Code States 14~15일차  (0) 2021.10.27
Code States 12일차  (0) 2021.10.22
Code States 11일차  (0) 2021.10.21
Code States 10일차  (0) 2021.10.20