JSFiddle - React, Tailwind, and code Playground

by mejileben

JavaScript

const namespace = (code, val) => {
	const baseAry = code.split('.')
  const obj = {}
  const recursiveVariable = ary => {
    const target = ary.pop()
    // 一番最後に来るのをwindowに突っ込む
    if (ary.length === 0) {
      window[target] = obj
      return
    }
    if (Object.keys(obj).length) 
  }
  recursiveVariable(baseAry)
}

namespace('foo.bar.baz', 3);
console.log(foo.bar.baz)