starkoora / wanted-pre-onboarding-challenge-fe-1

64 stars 1 forks source link

[질문]API 분석 도전해보려하는데 처음부터 막히네요.. #29

Closed nicehyun closed 1 year ago

nicehyun commented 1 year ago
    /**
     * Starts a ruleset in order to apply multiple rule options. The set ends when `rule()`, `keep()`, `message()`, or `warn()` is called.
     */
    $: this;

    /**
     * Starts a ruleset in order to apply multiple rule options. The set ends when `rule()`, `keep()`, `message()`, or `warn()` is called.
     */
    ruleset: this;

    type?: Types | string;

    /**
     * Whitelists a value
     */
    allow(...values: any[]): this;

`

멘토님 예시 코드보면서 알게된 joi API 사용해보기 전 API를 직접 분석해보고 사용하려고 하는데 쉽지않네요..

혹시 allow(...values: any[]): this; 여기서 this가 무엇을 의미하는지 알 수 있을까여.. 이런식으로 this를 리턴한다는 것들이 많네요..

starkoora commented 1 year ago

@ish1610 this는 기본적으로 어떤 객체가 자기 자신을 지칭할 때 쓰이는 대명사라고 생각하시면 되는데요. 전체 코드를 보진 않아서 잘 모르겠으나 this를 리턴해야 자기 자신을 다시 리턴하면서 메서드 체이닝을 이어나갈 수 있을 것 같습니다.

익숙한 예시로 Promise.then()의 경우에도 then()의 리턴 값으로 원래 프로미스 자기 자신을 리턴해야 그 뒤에 then, catch, finally 등의 메서드를 다시 호출할 수 있는 형태가 됩니다.

nicehyun commented 1 year ago

앗 감사합니다! 바로 이해됐어요 ㅜㅠ❗️