javascript - How to define a recursive object with a single expression? -
for example, given object:
let foo = {foo:1}; let bar = {a:foo,b:foo}; we can define bar single expression writing as:
let bar = (($0)=>({a:$0={foo:1},b:$0}))() however, given object:
let o = {a:'perfect'}; o.circle = o; is possible recreate structure of o single expression?
this won't work:
(($0)=>($0={a:"perfect",circle:$0}))() because circle nested , $0 has been defined yet.
unless want solve getter properties, assignment unavoidable.
there kinds of ways that, within single expression:
let o = (o = {a:'perfect'}).circle = o; but recommend focus on clarity , use iife instead:
const o = (function(){ const x = {a:'perfect'}; x.circle = x; return x; }()); // more first solution without mutable `o` variable: const o = (x => x.circle = x)({a:'perfect'});
Comments
Post a Comment