알고리즘 문제를 풀려면 배열은 필수적으로 사용하게 된다.아무리 봐도 맞는 로직인데 답이 이상해서 보면 배열 문제였던 경우가 많았다. 그 과정에서 알게 된 JS 배열의 특성들을 기록하고자 한다.Array().fill() vs. Array.from() - 2차원 배열 만들기let arr1 = Array(3).fill([]);arr1[0].push('a');arr1[1].push('b');arr1[2].push('c');console.log(arr1); // [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]]Array(3).fill([]).fill([])은 전부 동일한 객체를 채운다. (js에서는 배열도 객체다.)즉, arr1[0], arr1[1], arr1[2] 모두..
TSConfig:tsconfig.json: 타입스트립트를 컴파일할 때 필요한 설정들을 관리하는 파일 tsc cmd$ tsc --init: tsconfig.json 파일 생성 $ tsc $ tsc logging.ts: tsconfig.json 파일에 정의된 옵션으로 컴파일 진행 $ tsc -w $ tsc logging.ts -w: ts코드가 수정될 때마다 자동으로 컴파일한 후 js 실행 tsconfig.js 예시outDir : 컴파일 시 js파일이 생성될 경로rootDir : 시작루트 경로ts 파일이 처음 위치한 곳부터 상위 폴더 지정된다.{ "compilerOptions": { "outDir": "./build", ..
Proto:JS는 프로토타입 기반의 언어로 모든 객체들이 상속 받기 위한 템플릿으로 prototype object 를 가진다.상속되는 속성, 메소드들은 객체가 아닌 prototype 속성에 정의 ( __proto__ )즉, JS에서 상속, 코드 재사용 등 OOP을 위해 사용하는 것! Proto 속성JS에서 모든 객체는 Object type을 상속한다.Object.prototype 으로 접근 가능/** Proto */const x = {};const y = {};console.log(x); // __proto__: Objectconsole.log(y); // __proto__: Objectconsole.log(x.toString());console.log(x.__proto__ === y.__proto_..
Utility Types:TS에서는 타입 변환을 쉽게 하기 위해 유틸리티 타입을 제공해준다. Readonly모든 프로퍼티를 읽기전용(readonly)으로 설정해준다.type ToDo = { title: string; description: string;};function display(todo: Readonly) { todo.title = 'jaja'; // error} Partial모든 프로퍼티의 타입을 optional로 지정해준다.type ToDo = { title: string; description: string; label: string; priority: 'high' | 'low';};function updateTodo(todo: ToDo, fieldsToUpdate: Part..
Index Signatures:JS(TS)에서 Object에 들어있는 다른 객체를 참조할 때 문자열로 접근할 수 있다.const obj = { name: 'ellie',};obj.name; // ellieobj['name']; // ellie type Animal = { name: string; age: number; gender: 'male' | 'female';};// 예시1.type Name = Animal['name']; // stringconst text: Name = 'hello';const text2: Name = 1; // error// 예시2.type Gender = Animal['gender']; // 'male' | 'female'// 예시3.type Keys = keyof A..
22.09.19 월Type Alias와 Interface:Type vs Interface (기술 측면)type PositionType = { x: number; y: number;};interface PositionInterface { x: number; y: number;} 1. object (type) : 둘 다 가능// object ★const obj1: PositionType = { x: 1, y: 1,};const obj2: PositionInterface = { x: 1, y: 1, z: 1,}; 2. class (implements) : 둘 다 가능 // class ★class Pos1 implements PositionType { x: number; y: number;..
