js 实现根据参数返回对象内容的函数(类似i18n.t)
let objs = { a: { text: 'a', b: { text: 'b', c: { text: 'c', d: { text: 'd' } } } } } function getText (name, obj = objs) { if (!name) return let keys = name.split('.') let text = keys[0] if (keys.length === 1) { return obj[name] } else { keys.shift() let names = keys.join('.') return getText(names, obj[text]) } } console.log(getText('a.text')) // a console.log(getText('a.b.text')) // b console.log(getText('a.b.c.text')) // c