ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

์ฝ”๋“œ

export class InstantSearch {
    constructor(configuration) {
        this.DEFAULT_DELAY_TIME = 500;

        (...)

        this.delayTime = (configuration.delayTime === null || configuration.delayTime === undefined) ? this.DEFAULT_DELAY_TIME = 500 : configuration.delayTime;
    }

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด state ๊ฐ’์„ ์ „๋‹ฌ ๋ฐ›์•„ ํ•ด๋‹น ๊ฐ์ฒด ๋‚ด ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ์‹œ๊ฐ„ ๊ฐ’์ด ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด ์ธ์Šคํ„ด์Šค์˜ ๊ธฐ๋ณธ ์‹œ๊ฐ„์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ด์ฃผ๋Š” ๊ณผ์ •์„ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

(์ฐธ๊ณ ๋กœ delayTime์€ Input Value ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ณ€์ˆ˜์ด๋‹ค)

 

๋ฌผ๋ก  ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ๋„ ๋กœ์ง์—๋Š” ๋ฌธ์ œ๋Š” ์—†์œผ๋‚˜, ์šฐ์—ฐ์น˜ ์•Š๊ฒŒ ๋‹ค๋ฅธ ๋ถ„๊ป˜์„œ ๋„(null) ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•œ ๋ธ”๋กœ๊ทธ ๋‚ด์šฉ์„ ๋ณด๊ณ , ํ•ด๋‹น ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ฝ”๋“œ๋ฅผ ๋”์šฑ ๋ช…ํ™•ํ•˜๊ณ  ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด์•˜๋‹ค.

 

Nullish coalescing operator

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

 

Nullish coalescing operator - JavaScript | MDN

๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž (??) ๋Š” ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ null ๋˜๋Š” undefined์ผ ๋•Œ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์ด๋‹ค.

developer.mozilla.org

The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

 

null ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž๋Š” ES2020์— ์ถ”๊ฐ€๋œ ์ตœ์‹  ๋ฌธ๋ฒ•์ด๋‹ค.

๋ฆฌํ„ฐ๋Ÿด ??๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์งง์€ ๋ฌธ๋ฒ•์œผ๋กœ ์—ฌ๋Ÿฌ ํ”ผ์—ฐ์‚ฐ์ž ์ค‘ ๊ทธ ๊ฐ’์ด ํ™•์ •๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž.

a ?? b
a !== null  &&  a !== undefined
=> a
else
=> b

 

์ฆ‰, ์•ž์— ์žˆ๋Š” ๋ณ€์ˆ˜(left)๊ฐ€ null๋„ ์•„๋‹ˆ๊ณ  undefined๋„ ์•„๋‹ ๊ฒฝ์šฐ์— a์˜ ๊ฐ’์„ ํ• ๋‹น, ๊ทธ ์™ธ์—๋Š” b์˜ ๊ฐ’์„ ํ• ๋‹นํ•˜๋„๋ก ๋™์ž‘ํ•œ๋‹ค.

๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•œ ์‹์œผ๋กœ ๊ฐ’์˜ ํ• ๋‹น์„ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฐ์‚ฐ์ž ์šฐ์„  ์ˆœ์œ„๊ฐ€ 5๋กœ ๊ฝค ๋‚ฎ๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์žกํ•œ ์—ฐ์‚ฐ์‹์—์„œ ์œ ์˜ํ•˜๋ฉฐ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 

๋ฆฌํŒฉํ† ๋ง

export class InstantSearch {
    constructor(configuration) {
        this.DEFAULT_DELAY_TIME = 500;

        (...)

        // this.delayTime = (configuration.delayTime === null || configuration.delayTime === undefined) ? this.DEFAULT_DELAY_TIME = 500 : configuration.delayTime;
        this.delayTime = configuration.delayTime ?? this.DEFAULT_DELAY_TIME;
    }

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ