๐ฐobject destructuring
JS โฉ operator โฉ assignment โฉ destructuring โฉ object
let opts = { title: "Menu", width: 100, height: 200};
// โญ object destructuring
let {title, width, noSuchThing} = opts; // noSuchThing = undefinedโ
// โญ rename variables
let {title: t, width: w} = opts; // t = "Menu", w = 100
// โญ default values (could be function valuesโ)
let {width: w2 = 100, title: t2} = opts; // t2 = "Menu", w2 = 100
// โญ the "rest" object
let {title: t3, ...rest} = opts; // rest = {width: 100, height: 200}
๐ replit
replit โฉ object destructuring โฉ catch
// โญ๏ธ ๅฏๅ
ๅฎฃๅ่ฎๆธ๏ผๅ destructuring๏ผไฝ่ฆใๅฐๅฟใโ
let t, w, h;
// ------------------ ๐งจโthere's a catchโ --------------------
// โ SyntaxError: Unexpected token '='
// {t, w, h} = opts;
// ^^^^^^^^^ <------------ JS sees this as a "code block"โ
// ------------------------------------------------------------
// โ
wrap it in "parentheses", now it's OK.
({title: t, width: w, height: h} = opts);
// ^ ^
Last updated
Was this helpful?