๐Ÿฆ“ ์ •์˜๋˜์ง€ ์•Š์€ ์†์„ฑ์ด ์—†๋Š” Infinite ๊ฐ์ฒด๋ฅผ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๊นŒ!? (๊ฐœ์ •)

7677 ๋‹จ์–ด javascriptcodepen
๐Ÿ˜… ๋งŒ์•ฝ... ๋น›์„ ๊ด€์ฐฐํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ํŒŒ๋™์ด๋‚˜ ์ž…์ž๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์˜ค ๋ณผ ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ข‹์•„์š”, ๋ฌผ์ฒด์˜ ์†์„ฑ์„ ์–ป์Œ์œผ๋กœ์จ ๊ทธ ์†์„ฑ์ด ์ƒ๊ฒจ๋‚˜๊ฒŒ ํ•œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”?

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

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

์›๋ณธ์„ ์ž‘์„ฑํ•œ ์ง€ ๋ช‡ ๋…„์ด ์ง€๋‚ฌ์ง€๋งŒ ์˜ค๋Š˜ ๋ช‡ ๊ฐ€์ง€ ๋ชจ๋ฒ” ์‚ฌ๋ก€๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์‹œ์ ์—์„œ ๊ทธ๊ฒƒ์€ ๋‚˜์—๊ฒŒ ๋ชจ๋ฒ” ์‚ฌ๋ก€ ๐Ÿ˜œ๊ฐ€ ์•„๋‹Œ ๋งˆ์ˆ ์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

const infinieSet = (obj) => {
    return new Proxy(obj, {
        get(tar, prop){
            if(!Object.prototype.hasOwnProperty.call(tar, prop)){
                tar[prop] = {};
            }
            return infinieSet(tar[prop]);
        },
    })
}


์šฉ๋ฒ•:




const infinite = infinieSet(target);

// pointless destructure works XD
const {some: {obj: {magic}}} = infinieSet(target);
console.log(magic); // {}

// or

infinite.this.is.not.defined = 'ummm'
infinite.and.yet.it.is = 'huh?'
infinite.the.potential.is.endless ='what is it for?'
infinite
    .template
    .locals
    .could
    .be
    .easier
    .to
    .extend
    .if
    .you
    .could
    .just
    .do
    .this = 'but it must have a value'

// or even from a string
eval('infinite.x.y.z');

/*
{
    "some": {
        "obj": {
            "magic": {}
        }
    },
    "this": {
        "is": {
            "not": {
                "defined": "ummm"
            }
        }
    },
    "and": {
        "yet": {
            "it": {
                "is": "huh?"
            }
        }
    },
    "the": {
        "potential": {
            "is": {
                "endless": "what is it for?"
            }
        }
    },
    "template": {
        "locals": {
            "could": {
                "be": {
                    "easier": {
                        "to": {
                            "extend": {
                                "if": {
                                    "you": {
                                        "could": {
                                            "just": {
                                                "do": {
                                                    "this": "but it must have a value"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "x": {
        "y": {
            "z": {}
        }
    }
}
*/


์ข‹์•„ ๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ์„œ ๋ฌด์Šจ ์ผ์ด์•ผ?



๋ฉ”ํƒ€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ •์˜๋˜์ง€ ์•Š์€ ์†Œํ’ˆ์ด ์ด๋ฏธ ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์•ก์„ธ์Šค(์š”์ฒญ ์‹œ ์ƒ์„ฑ)๋˜๋„๋ก ํ—ˆ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์žฌ๊ท€ ํ”„๋ก์‹œ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ๊ณผ ์•ฝ๊ฐ„ ๋น„์Šทํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ์ฃผ์š” ์ฐจ์ด์ ์€ ์ฆ‰์„์—์„œ ์ •์˜๋œ ๊ฒƒ์˜ ์ถœ๋ ฅ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ์™œ ์ด๋Ÿฌ์ง€?



๋‚ด๊ฐ€ ๊ณผ์‹œํ•˜๊ณ  ์žˆ์—ˆ์ง€๋งŒ ์ด๊ฒƒ์ด ์ˆ˜๋…„ ๋™์•ˆ ์œ ์šฉํ•˜๋‹ค๋Š” ๊ฒƒ์ด ๋ฐํ˜€์กŒ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ์ด๊ฒƒ์€ ํ›จ์”ฌ ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค(ํŒŒ์ผ ์‹œ์Šคํ…œ๊ณผ ๊ฐ™์€ ๊นŠ์€ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ๋” ์ข‹์Šต๋‹ˆ๋‹ค). ๊ทธ๋•Œ ๋‚˜๋Š” ์ด๋ ‡๊ฒŒ ๋งํ–ˆ๋‹ค.

I am building an api in express, I don't want to return undefined or cause template errors that crash the page, instead I want to give the option on a special locals object to dynamically assign props to the api and if not defined (although they are), show useful errors as a reaction rather than a catch.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ