๐ฆ ์ ์๋์ง ์์ ์์ฑ์ด ์๋ Infinite ๊ฐ์ฒด๋ฅผ ์ด๋ป๊ฒ ์์ฑํ์ต๋๊น!? (๊ฐ์ )
์ค๋ ์ ์ ๋๋ ์ 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.
Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐ฆ ์ ์๋์ง ์์ ์์ฑ์ด ์๋ Infinite ๊ฐ์ฒด๋ฅผ ์ด๋ป๊ฒ ์์ฑํ์ต๋๊น!? (๊ฐ์ )), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://dev.to/adam_cyclones/es6-object-property-of-undefined-not-anymore-revised-26a7ํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
                                
                                
                                
                                
                                
                                ์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ  ๋ฐ๊ฒฌ์ ์ ๋
                                (Collection and Share based on the CC Protocol.)
                            
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค