개발집사 2022. 7. 13. 15:58

console.log(1+'1') //11
console.log(1+ true) //2
//true =1 , false = 0
console.log(122- '1') //121
console.log('1'+true) //1true
 
레퍼런스
함수 선언식(declaration) vs 함수 표현식(expression)
console.log(typeof funcDeclared) //'function'
console.log(typeof funcExpressed) //'string'

function funcDeclared() {
return 'this is a function declaration';
}

funcExpressed = function () {
return 'this is a function expression';
};

const funcContainer = { func: funcExpressed };
expect(funcContainer.func()).to.equal('this is a function expression');

funcContainer.func = funcDeclared;
expect(funcContainer.func()).to.equal('this is a function declaration');
});
 
함수 호이스팅
인터프리터가 변수와 함수의 메모리 공간을 선언 전에 미리 할당하는 것을 의미합니다.
var로 선언한 변수의 경우 호이스팅 시 undefined로 변수를 초기화합니다. 반면 
let과 const로 선언한 변수의 경우 호이스팅 시 변수를 초기화하지 않습니다. - MDN

변수의 선언과 초기화를 분리한 후, 선언만 코드의 최상단으로 옮기는 '현상'

 

spread syntax
const arr1 = [0, 1, 2];
    const arr2 = [3, 4, 5];
    const concatenated = [...arr1, ...arr2];
    expect(concatenated).to.deep.equal([0, 1, 2, 3, 4, 5]);
    // 아래 코드도 같은 동작을 수행합니다.
    //  arr1.concat(arr2);

 

얕은 복사 vs 깊은 복사

얕은 복사

객체를 복사할 때 원래값과 복사된 값이 같은 참조를 가리키고있는 것.

객체 안에 객체가 있을 경우 한개의 객체라도 원본 객체를 참조하고 있다면 얕은 복사.

1.Object.assign()

2.전개연산자

 

 

깊은 복사

객체안에 객체가 있을 경우에도 원본과의 참조가 완전히 끊어진 객체

1.재귀함수를 이용한 복사

2.JSON.stringify()

3.라이브러리사용