본문 바로가기

Web development/Node.js & Typescript22

[JSDoc] Practical commenting out with JSDoc If we write code with proper naming and good readability, we might argue that comments are unnecessary because the code itself explains everything. However, there are times when we need to explain aspects of the code that are external to the project. Additionally, there may be similar functions that have slightly different functionalities (although it's better to avoid such situations). For exam.. 2023. 5. 24.
[Javascript] How to Iterate through Lists in JavaScript Here is a basic way to iterate through a list in JavaScript: const list = [1, 2, 3]; for (var i = 0 ; i < list.length ; i++){ console.log(list[i]); } Alternatively, you can use the of keyword instead of using an index: const list = [1,2,3] for(const item of list){ console.log(item); } for (const a of arr) console.log(a); // You can also use inline syntax You can create arrays using square bracke.. 2023. 5. 24.
[JSDoc] 유용한 주석 달기 올바른 네이밍과 가독성 좋게 코드를 작성한다면, 코드 그 자체로 모든 것이 설명되므로 주석은 불필요하다고 할 수도 있겠다. 그러나 가끔 해당 프로젝트 코드 외적인 이유를 설명해야 할 때도 있고, (이런 일이 없는 것이 더 좋긴 하겠지만) 비슷하지만 약간 다른 기능을 하는 함수가 존재하는 경우도 있을수 있다. 예를들어... 아래와 같은 경우가 있을 수 있다. 피시방에서 쓰는 프로그램을 만드는데, 내가 프론트 작업을 해야 한다. 근데 백엔드에서 데이터를 아래와 같이 보내주고 있었다. [ { id: 1, name: '10시간 이용권', count: 10, price: 10000, }, { id: 2, name: '20시간 이용권', count: 20, price: 15000, }, ]; 그런데... 피시방에서.. 2022. 9. 6.
ES Module Export (Default, Named) 모듈 시스템을 사용하는 이유는, 쪼개져있는 코드들을 효율적으로 불러다 쓰기 위해서다. module을 잘 활용하면 깔끔하고 효율적인 코드를 작성할 수 있다. Named Export - 지정된 이름을 사용하는 export - 한 파일에서 여러 객체를 export할때 사용 // exp.js export const num = 100; export const foo = (a) => a + 1; export class bar {}; // imp.js import { num, foo, bar } from './exp.js'; import시 정의된 것과 이름을 사용해야 하며, 변경을 원하는 경우 import { foo as f } 로 사용할 수 있다. 한번에 전체 export를 가져오고 싶은 경우 // imp.js i.. 2021. 9. 28.
[Javascript] 객체 수정시 원본이 변경되는 문제(얕은 복사와 깊은 복사) 자바스크립트의 값의 종류는 원시값과 참조값이 존재한다. Boolean, String, number, null, undefined : 원시값 Object, Array : 참조값 원시값의 경우 새로운 변수에 할당시 값 자체가 할당되고, 참조값은 값의 주소가 할당된다. 그러므로 새로운 변수에 할당 후 수정하면, 객체나 배열의 경우 원본의 값이 변경되어버린다. const obj = {'a': 1, 'b': 2}; const tempObj = obj; tempObj.a = 3; console.log(obj); // {'a': 3, 'b': 2} 문제 문제는, formData의 변경이 필요할때 등 원본을 보존하는 복사가 필요한 경우가 생긴다. 예를 들면, 아래와 같은 form 데이터가 있다. formData = {.. 2021. 5. 6.
[Typescript] 유틸리티 타입 - Parameters, ReturnType, Required Parameters 함수 타입 T의 매개변수 타입을 튜플 타입으로 정의한다. declare function createCat(cat: {color: string, legs: number}): void type catParams = Parameters; // [{color: string, legs: number}] ReturnType 함수 T의 반환 타입으로 정의한다. Parameters와 대칭되는 형태라고 볼 수 있겠다. declare function createCat(): Cat type catResult = ReturnType; // Cat Required T의 모든 속성이 필수인 타입을 만든다. optional로 설정한 속성도 반드시 가지고 있어야 한다. interface User { name: s.. 2020. 8. 1.
[Typscript] 유틸리티 타입 - Pick, Omit Pick 일부의 속성만 가질 수 있을 때 사용한다는 점에서 Partial과 비슷하지만, 속성을 직접 선택한다는 점이 다르다. interface Post { title: string; author: string; content: string; isHidden: boolean; } // 게시글을 작성한다. const newPost = { title: 'hello world', author: 'effy', content: 'hello', isHidden: false, } // 제목과 내용만 바꿀 수 있도록 하고 싶다. function updateOnlyTitleAndContent(post: Post, payload: Pick) { return {...post, ...payload} } updateOnlyTit.. 2020. 7. 22.
[Typscript] 유틸리티 타입 - Partial, Readonly, Record Partial 일부의 속성만 가질 수 있을 때 사용한다. 예를 들어 게시글 인터페이스엔 제목, 글쓴이, 내용 등등이 있을 것이다. 게시글 수정시 항상 모든 필드를 수정하지는 않는다. 게시글에 포함된 프로퍼티 중 일부의 프로퍼티(하위 타입 집합)만 사용하고 싶을 때 사용할 수 있다. interface Post { title: string; author: string; content: string; isHidden: boolean; } // 게시글을 작성한다. const newPost = { title: 'hello world', author: 'effy', content: 'hello', isHidden: false, } // 수정하고 싶어졌다. 제목만 바꿀수도 있고, 내용만 바꿀수도 있고, 다 바꿀수도 .. 2020. 7. 21.
var vs let vs const var let const 스코프 함수 블록 블록 변수 재선언 O X X 재할당 O O X 할당하지 않고 선언만 하기 O O X 변수 선언 전 호출시 undefined ReferenceError ReferenceError 2020. 7. 19.