如何在 TypeScript 中优雅地初始化一系列对象?

huangapple go评论78阅读模式
英文:

How to gracefully initialize a series of objects in TypeScript?

问题

我需要在我的代码中临时创建一个对象,格式类似于 one.two.three.four

但是我知道的初始化方式很繁琐。有没有办法可以用少量代码来优雅地初始化它们?

我现在正在做的很丑陋:

let one: any;

one = {};

one.two = {};

one.two.three = {};

console.log(one.two.three);
英文:

I need to temporarily create an object in format like one.two.three.four in my code.

But the way I known to initialize them is annoying. Is there anyway I can gracefully initialize them with few lines of code?

What I am doing now is ugly:

let one: any;

one = {};

one.two = {};

one.two.three = {};

console.log(one.two.three);

答案1

得分: 1

如克里斯建议的那样,我们可以简单地这样做:

let one: any = {two: {three: {four:false}}}
英文:

As Chris suggested, we can simply do it by:

let one: any = {two: {three: {four:false}}}

huangapple
  • 本文由 发表于 2020年1月3日 14:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定