티스토리 뷰
반응형
클래스를 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);
expect(robot).toHaveProperty('arm', arm);
expect(robot).toHaveProperty('leg', leg);
expect(robot).toHaveProperty('head', head);
expect(robot).toHaveProperty('body', body);
});
});
class Robot {
#arm;
#leg;
#head;
#body;
static #builder = class RobotBuilder {
#arm;
#leg;
#head;
#body;
setArm(arm) {
this.#arm = arm;
return this;
}
setLeg(leg) {
this.#leg = leg;
return this;
}
setHead(head) {
this.#head = head;
return this;
}
setBody(body) {
this.#body = body;
return this;
}
build() {
const param = {};
if(this.#arm) param.arm = this.#arm;
if(this.#leg) param.leg = this.#leg;
if(this.#head) param.head = this.#head;
if(this.#body) param.body = this.#body;
return new Robot(param);
}
}
static get builder() {
return new this.#builder();
}
constructor({ arm, leg, head, body }) {
this.#arm = arm;
this.#leg = leg;
this.#head = head;
this.#body = body;
console.log(`create robot with`, arm, leg, head, body);
}
get arm() { return this.#arm };
get leg() { return this.#leg };
get head() { return this.#head };
get body() { return this.#body };
}
'Dev. > javascript' 카테고리의 다른 글
[JS] 값이 null 또는 undefined인지 확인하기 (0) | 2022.10.04 |
---|---|
[JS] 객체 배열을 문자열 기준으로 정렬하기 (0) | 2022.09.19 |
[TS] any와 unknown의 차이 (2) | 2022.08.28 |
[JS] async는 프로미스를 반환한다. (0) | 2022.06.02 |
[JS] Function.prototype.bind() (0) | 2022.05.27 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- eureka
- Git
- Linux
- Gradle
- Java
- Docker
- kotlin
- hashicorp
- Spring Cloud Config
- Vault
- AWS
- jQuery
- spring boot
- terraform
- react
- AWS IoT
- Database
- RancherDesktop
- shell
- pgloader
- javascript
- IntelliJ
- mysql
- 코딩테스트
- JPA
- JEST
- Node.js
- postgresql
- Puppeteer
- etc
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함