본문 바로가기

TIL/Node JS22

module.exports, 비구조화 할당 //모듈은 여러번 재 사용될 수 있다. const checkNumber = require('./func') //변수명도 바꿀 수 있다. //checkNumber === checkOddOrEven module.exports = checkOddOrEven; // 함수를 내보냄. // module.exports => // 1234 // true // 함수도 내보낼 수 있다. // 아무거나 내보내도 상관 없다. // 비구조화 할당 사용 const { odd, even } = require('./var') ' // { odd, even }; 다른곳에서 사용할 수 있도록 내보내준다. module.exports = { odd, // odd : odd, even // even : even, }; //module.exp.. 2019. 10. 20.
비구조화 할당 (destructuring) const a = 객체.a; const b = 객체.b; // 다음과 같이 바꿀 수 있다. const { a, b } = 객체 ; const { status, getCandy } = candyMachine; candyMachine라는 객체에서, status랑 getCandy를 변수로 꺼내옴. const { Router } = require('express'); // require('express')객체에서, Router라는 변수로 꺼내옴 const array = ['node', {}, 10, true]; const [node, obj, , bool] = array; //const [node, obj, ...bool] = array; 2019. 10. 19.
const 바뀐점. if(true){ const y = 3; } console.log(y); const 특성상 블럭 바깥으로 빠져나갈 수 없어서 에러가 뜬다. const a = 0; a = 1; const는 값 재할당 불가 하지만, const에 객체가 할당된 경우, 객체 내부속성은 바꿀 수 있다. ex) const g = { a:1, b:2, c:3}; g.a = 3; g.b = 5; g = { a:3, b:5, c:3}; ex) const h = [1,2,3,4]; h[0] = true; h[1] = false; h = [true, false, 3, 4] const는 메모리 주소에 대한 상수, 참조에 대한 상수. 2019. 10. 19.