클래스를 const varName = class ClassName {} 형태로 작성할 수 있는 점을 다음과 같이 이용할 수 있다. 'use strict'; describe('my-tests', () => { it('playground', async () => { const arm = 'add one arm'; const leg = 'add two legs'; const head = 'add small head'; const body = 'add big body'; const robot = Robot.builder .setArm(arm) .setLeg(leg) .setHead(head) .setBody(body) .build(); expect(robot).toBeInstanceOf(Robot); expec..
아래와 같은 코드가 있다고 하자. type User = { id: number; name: string; age: number; } // 프로미스를 반환한다. async function getUserAsync(id: number): Promise { const user: User = { id, name: 'ingnoh', age: 3, }; return new Promise((resolve => { setTimeout(() => { resolve(user); }, 2000); })); } (async function main() { console.log('main start!!!'); // 첫 번째로 출력된다. const user = await getUserAsync(1); console.log(user..
함수에 대해 호출된 bind()는 인자로 this를 전달한다. function temp () { return this.x; } console.log(temp()); console.log( temp.bind({ x: 42 })() ); /* 실행 결과 undefined 42 */ bind() 의 두 번째 인자부터는 binding 대상 함수의 인수로 전달된다. function temp (prop) { return this[prop]; } console.log(temp('x')); console.log( temp.bind({ x: 42 }, 'x')() ); /* 실행 결과 undefined 42 */
const date = new Date(); const day = date.getDate(); const date2 = new Date(); date2.setDate(day + 5); const date3 = new Date(); date3.setDate(day + 3) const date4 = new Date(); const arr = [date2, date4, date, date3]; console.log( arr.sort( (a, b) => b - a // 오름차순 정렬은 (a, b) => a - b ) ); /* 실행 결과 [ 2022-05-22T05:28:29.877Z, 2022-05-20T05:28:29.877Z, 2022-05-17T05:28:29.877Z, 2022-05-17T05:28:2..
const arr1 = [1, 2, 3]; const arr2 = [1, 2, 3]; const sliced = arr1.slice(0, undefined); const spliced = arr2.splice(0, undefined); console.log(`original: ${arr1} / sliced: ${sliced}`); console.log(`original: ${arr2} / spliced: ${spliced}`); /* 실행 결과 original: 1,2,3 / sliced: 1,2,3 original: 1,2,3 / spliced: */ slice: 원본 배열에서 인수에 전달된 범위를 잘라오지만, 원본 배열은 수정하지 않는다. splice: 원본 배열에서 인수에 전달된 범위만큼 자르며, ..
참고 Private class fields - JavaScript | MDN class 의 속성(property)들은 기본적으로 public 하며 class 외부에서 읽히고 수정될 수 있다. 하지만, ES2019 에서는 해쉬 # prefix 를 추가해 private class 필드를 선언할 수 있게 되었다. developer.mozilla.org 아래와 같은 클래스가 있다고 하자. class Runner { constructor(name, age) { this.name = name; this.age = age; } sayHello() { /* 엄 청 나 게 길 고 가 독 성 이 떨 어 지 는 로 직 */ console.log('comments end'); console.log(`Hello, my name..
class Parent { constructor(name) { this.name = name; } get who() { return this.name + ' is parent'; } } class Child extends Parent { constructor(name) { super(name); } get who() { // return super.who; return this.name + ' is child'; } } const parent = new Parent('parent'); const child = new Child('child'); console.log(parent.who); console.log(child.who); 그냥 똑같은 이름의 메소드를 자식 클래스에서 재정의하면 된다. 부모 클래스..
const animals = [ { name: 'doggo', age: 0 }, { name: 'catch', age: 3 }, { name: 'birdy', age: 1 }, ]; const map = new Map(animals.map(animal => [animal.name, animal.age])); console.log(map); [JS] 배열을 map으로 변환하기 자바스크립트 array 를 object, Map 으로 변환하기 코틀린을 쓰다보면 associate 메소드를 이용하여 리스트를 맵으로 쉽게 바꾸어 사용합니다. js, ts 에서는 코틀린의 associate 처럼 메소드로 제공되어 있 ingnoh.tistory.com 기존에 작성한 위 방식보다 더 직관적인 듯!
- Total
- Today
- Yesterday
- terraform
- AWS IoT
- react
- Spring Cloud Config
- Docker
- eureka
- shell
- jQuery
- RancherDesktop
- Database
- postgresql
- javascript
- IntelliJ
- mysql
- pgloader
- Java
- Git
- JEST
- hashicorp
- AWS
- etc
- spring boot
- Vault
- Linux
- Node.js
- JPA
- Gradle
- Puppeteer
- 코딩테스트
- kotlin
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |