Lesson 11 / الدرس 11

Destructuring / التفكيك

Pulling values out of an object or an array into named variables, in one line. It appears everywhere in modern code, and once you can read it a great deal of other people's code opens up.

سحب القيم من كائن أو مصفوفة إلى متغيّرات مسمّاة في سطر واحد. ويظهر في كل مكان في الشيفرة الحديثة، وما إن تستطيع قراءته حتى ينفتح لك كثير من شيفرة الآخرين.

The same thing, shorter

const name = product.name;              before
const price = product.price;

const { name, price } = product;        after

const { name, price = 0 } = product;    with a default if missing
const { name: title } = product;        under a different name

const [first, second] = list;           arrays, by position
The names on the left must match the keys, which is the whole rule for objects. For arrays it is position instead, since arrays have no names. Run the demonstration.

Where it earns its place

SituationWhy it helps
In a function parameter listThe call site names every value, in any order
In a for … of over objectsThe loop body reads without dots
Taking two values from a responseOne line instead of three
Importing from a moduleThe same syntax — lesson 14
Everything from one objectDo not — see the warning

The first row is the one worth adopting. It answers the complaint from lesson 19 of the basics course: a function with six positional parameters cannot be called correctly without checking the definition, and one that destructures an object names every value where it is called.

Two smaller notes. Destructuring more than about four values in one line stops being readable — at that point taking the object itself and using dots is clearer. And renaming with { name: title } reads backwards the first few times: the key is on the left and your new variable name is on the right, which is the opposite of an assignment.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What does const { name, price } = product do?

2. Why does const { name } = order.customer sometimes crash?

3. Where does destructuring help most?

Your task / مهمتك

Take three functions from earlier in this course that take several parameters and rewrite them to destructure a single object instead, showing a call to each before and after. Then loop over a list of objects using destructuring in the loop header. Finally, cause the undefined crash on purpose, show the message, and fix it two different ways.

خذ ثلاث دوال من هذه الدورة تأخذ عدة وسائط وأعد كتابتها لتفكك كائنًا واحدًا بدلًا منها، مع إظهار استدعاء لكل واحدة قبل وبعد. ثم دُر على قائمة كائنات مستخدمًا التفكيك في ترويسة الحلقة. وأخيرًا تسبب في انهيار undefined عمدًا، وأظهر الرسالة، وأصلحه بطريقتين مختلفتين.

  • Three functions rewritten, calls shown both ways ثلاث دوال معادة، والاستدعاءات بالطريقتين
  • A loop destructuring in its header حلقة تفكك في ترويستها
  • At least one default value used قيمة افتراضية واحدة على الأقل مستخدمة
  • The crash reproduced and fixed two ways الانهيار مُعاد إنتاجه ومُصلَح بطريقتين
How do you want to submit? / كيف تريد التسليم؟