Home > OS >  Adding deep nested object properties to Window in JS
Adding deep nested object properties to Window in JS

Time:03-05

I would like to clean up this block of code. Is there a way of setting deep object properties without using Lodash, Ramda or some horrible method that splits the object property string and loops through it?

export const initialiseBlackbox = (value = '') => {
    if (window === undefined) {
        window = { IGLOO }
    }

    if (window.IGLOO === undefined) {
        window.IGLOO = {}
    }    

    if (window.IGLOO.getBlackbox === undefined) {
        window.IGLOO.getBlackbox = () => ({ blackbox: value })
    }
}

CodePudding user response:

Sure, but it's not pretty:

export const initialiseBlackbox = (value = '') =>
    Object.assign(window.IGLOO || (window.IGLOO = {}),
    { getBlackbox: () => ({ backbox: value }) });
  • Related