유닛테스트를 배워보는 과제
expect syntax
expect에는 테스트할 실제 값을 예상되는 값 matchers와 비교하여 같이 사용한다.
matchers
let sum1 = 1+1;
let value = 2;
expect( sum1 === value).toBeTruty();
expect(sum1).toBe(value);
toBe vs toEqual
expect([1,2,3] === [1,2,3]).toBe(false); // 주소값이 다르기 때문에 false
expect([1,2,3]).toEqual([1,2,3]); // 주소값이 달라도 배열의 요소가 같은지 확인해준다.
0 == false // true, 데이터 타입에 신경 쓰자. 0은 숫자, false는 boolean이다.
'' == false
[] == false
![] == false
[] == ''
[] == 0
[''] == ''
[''] == 0
[0] == 0
출처 : https://hyunseob.github.io/2016/11/21/misunderstanding-about-const/
출처 : 변수 사용이 나쁘다 참고
함수를 어디서 선언했는지에 따라 상위 scope의 차이를 말하는 것이다. 여기서 중요한 점은 함수의 호출이 아니라 함수의 선언에 따라 결정된다는 점이다.
다음 예시를 보고 명확히 이해해보자.
var number = 1;
function a() {
var number = 10;
b();
}
function b() {
console.log(number);
}
a(); // 1
b(); // 1
함수의 호출 시 상위 scope가 결정된 것이 아니라 함수의 선언에 따라 상위 scope가 결정되었기 때문이다.
출처 : https://medium.com/@yeon22/javascript-lexical-scope-static-scope-and-dynamic-scope-c4a9e941fab3
mdn에서 클로저 정의
A closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time the closure was created.
클로저는 함수와 함수가 선언된 어휘적 환경의 조합을 말한다.
내부 함수가 외부 함수의 지역 변수에 접근할 수 있고 외부함수는 내부함수(closure)의 지역 변수에 접근 불가능하다.
fucntion factory
factory function
업데이트 예정
namespacing private variables/functions
업데이트 예정
배열 및 객체 합치기
let arr = [...[1,2,3],...[4,5,6]]
console.log(arr) // [1,2,3,4,5,6]
let obj = {...{one:1}, ...{two:2}} // deep copy
console.log(obj) //{one:1,two:2}
function restParameterFunc(...rest){
return rest;
}
function argumetsFunc(){
return arguments;
}
restParameterFunc(1,2,3) === argumetsFunc(1,2,3) // 결과 : false
Array.isArray(restParameterFunc(1,2,3)) //결과 : true
Array.isArray(argumetsFunc(1,2,3)) // 결곽 : false, 유사배열
유사 배열을 배열로 만들기
const argsArr = Array.from(argumetsFunc(1,2,3));
Array.isArray(argsArr); // 결과 : true