Spec types: Completion, Property Descriptor & more
Read ECMAScript's bookkeeping types through small JavaScript examples: follow completions, inspect property flags, and recognize lists, closures, and bytes.
- 01Follow how a step finishesRead a Completion Record to predict what
return,throw,break,continue, andfinallydo. - 02Check a property's flagsTell a data property from an accessor property, and test the
writable,enumerable, andconfigurableflags. - 03Name the other spec valuesFind Lists, Records, Abstract Closures, and Data Blocks in the spec, and know that your code never holds one.
The spec has its own words
You can put JavaScript values in variables and print them. The ECMAScript specification is the official rulebook for JavaScript. People often call it "the spec". The spec also uses its own helper values to explain, step by step, how JavaScript works. It calls them specification types, or spec types. You can't make them or use them in your own code.
const cart = { total: 3 };
console.log(cart.total);Line 1 makes a cart object with a total of 3. Line 2 reads cart.total and prints 3. While line 2 runs, the spec also makes a small note: "this step finished normally, with the value 3". That note is a spec type called a Completion Record. No JavaScript code can get hold of it.
A specification type is a helper value that only the spec's steps use. It can describe a result in the middle of a step. You can't store it in a variable or in an object property.
In How to read ECMAScript, you learned to follow the spec's numbered steps and its names in double square brackets, like [[Value]]. This lesson names the helper values those steps use:
- Completion Records say how a step finished.
- Property Descriptors hold the settings of one property.
- Lists keep values in order, and Records hold named fields.
- Abstract Closures are spec steps saved to run later.
- Data Blocks are raw bytes.
One more spec type, the Environment Record, keeps track of the variable names in each scope. It gets its own lesson later: Environment Records.
Completion Records: how a step finished
A Completion Record is the spec's note about how a step finished. It has three fields, or named parts:
[[Type]]says how the step finished: normally, with areturn, with athrow, with abreak, or with acontinue.[[Value]]holds the value the step made, if there is one.[[Target]]holds a label name, for abreakorcontinuethat jumps to a labelled loop. When there is no label, it holds EMPTY. EMPTY is the spec's word for "nothing here".
const price = 3;
console.log(price);Line 1 stores the value 3 in price. Line 2 prints 3. The spec says that reading price finished normally, with the value 3. The Completion Record itself is never printed. It is not an object in your program.
Spec steps read like a recipe. "Let x be …" means "call the result x". A ? before a step means "stop here and throw if this fails". The box below is one step from the spec's NormalCompletion operation. That operation makes the record for a normal finish.
1. Return Completion Record { [[Type]]: NORMAL, [[Value]]: value, [[Target]]: EMPTY }.In plain words, line by line:
- Make a Completion Record. Its type is NORMAL, its value is the value that was made, and its target is EMPTY.
You can read the full step in NormalCompletion. Remember that EMPTY means "nothing here" inside the spec. It is not the JavaScript value undefined.
The next example uses try and finally. The code in a finally block always runs when the try block ends, even when the try block returns. Step through it and watch the order.
A replay of the example code, step by step. It shows what a Completion Record describes. It does not show the engine's own records.
script
function chooseTea(available) { try { if (!available) throw new Error("sold out"); return "tea"; } finally { console.log("checked"); }}Line 3 checks whether tea is available. It is, so line 4 gets ready to return "tea". Before the function really ends, line 6 in finally prints checked. Then line 9 prints tea. So the output is checked, then tea. The replay shows the example code running step by step. It does not show the inside of a real JavaScript engine.
RETURN, and a value, "tea". Its target is empty. Your code never sees the record; the caller only gets tea.The table lists the five types a Completion Record can have. Read each row as: "when a step finishes this way, this happens next".
| Type | What happens next | Example |
|---|---|---|
| NORMAL | Go on to the next step, with the value the step made. | const price = 3; finishes, and the code carries on. |
| RETURN | Leave the function. The value goes back to the caller. | return "tea"; |
| THROW | Jump to the nearest catch. The value is the error. | throw new Error("sold out"); |
| BREAK | Leave the loop. | break; when the order is "done" |
| CONTINUE | Skip to the next round of the loop. | continue; when the order is "skip" |
At a food counter, you order and get a token. Later a screen shows your token with a message. The message says either "ready" or "sold out". The message tells you what to do next. The token tells you which order you get.
- In real life: The screen says your order is ready
- In JavaScript: NORMAL: carry on as usual
- In real life: The screen says the item is sold out
- In JavaScript: THROW: leave and look for a
catch - In real life: The token shows which order it is
- In JavaScript:
[[Value]]holds the result
Abrupt finishes jump somewhere else
An abrupt completion is any Completion Record whose type is not NORMAL. "Abrupt" means the code jumps somewhere else instead of going on to the next step:
throwjumps to the nearestcatch.returnleaves the function.breakleaves a loop.continueskips to the next round of a loop.
Abrupt does not mean something went wrong. A normal return is abrupt too, because it jumps out of the function.
function priceOrError(price) { if (price < 0) throw new Error("negative price"); return price;}try { console.log(priceOrError(-1)); }catch (error) { console.log(error.message); }Line 1 starts a function that checks a price. Line 2 throws an Error when the price is below zero, so line 3 never runs. Line 5 calls the function with -1. Line 6 catches the error and prints its message, negative price.
For that throw, the spec makes a Completion Record with type THROW. Its value is the Error object. Your code can see the Error object, but not the record around it.
The spec has a short way to say "if this step finished abruptly, stop and pass the record on". It writes ReturnIfAbrupt, or just the ? you met in the previous lesson.
let served = 0;for (const order of ["tea", "skip", "done"]) { if (order === "skip") continue; if (order === "done") break; served++;}console.log(served);Line 1 sets served to 0. Line 2 goes through "tea", "skip", and "done", in that order. Line 3 skips the rest of the round for "skip". Line 4 leaves the loop at "done". So line 5 counts only tea, and line 7 prints 1.
These two jumps have no label, so their [[Target]] is EMPTY. With a label, like break outer;, the target would hold the name outer.
How finally can replace a return
A finally block runs even while a return or throw is waiting to finish. What happens next depends on how the finally block itself ends:
- If it ends normally, the waiting
returnorthrowgoes ahead. - If it has its own
returnorthrow, that new one wins. The waiting one is thrown away.
So a return inside finally can hide an earlier result, or even an error.
function getStatus() { try { return "ready"; } finally { return "closed"; }}console.log(getStatus());Line 1 starts getStatus. Line 2 gets ready to return "ready". Line 3, in finally, returns "closed" instead. So line 5 prints closed. The word ready is never printed.
finally runs. The return on line 3 takes its place, so the console prints closed.Compare this with the tea replay earlier. Its finally block only printed checked. It had no return, so tea still reached the caller.
In real code, use finally for clean-up, like closing a file. Don't return from finally unless you really want to replace the result.
The same rule holds when try throws. If finally ends normally, the error goes on to the nearest catch. If finally throws its own error, the new error replaces the old one. To predict the result, ask: how did the finally block end?
Property Descriptors describe a property
Every property has a value and a few settings. A Property Descriptor is the spec's Record for one property's value and settings. A Record is a set of named fields. The settings are called flags, because each one is either true or false:
writable: can the value be changed with=?enumerable: does the property show up in lists of keys, likeObject.keys?configurable: can the property be deleted, or its flags changed?
Two JavaScript tools work with descriptors:
Object.defineProperty(obj, name, settings)adds a property with the settings you choose.Object.getOwnPropertyDescriptor(obj, name)gives back an ordinary object that shows one property's value and flags.
That ordinary object is made for your code. It is not the spec's own Record.
const order = {};Object.defineProperty(order, "status", { value: "ready" });console.log(Object.getOwnPropertyDescriptor(order, "status").writable);console.log(Object.keys(order).length);Line 1 makes an empty order. Line 2 adds a status property with the value "ready", and gives no flags. Line 3 prints false. When you leave a flag out, defineProperty sets it to false. Line 4 prints 0. The enumerable flag is false too, so Object.keys skips status.
A property made the usual way, with order.status = "ready", is different. All three of its flags are true.
status has its value and three yes-or-no flags. Line 2 gave only a value, so all three flags are false.The box below comes from the spec's FromPropertyDescriptor operation. It builds the ordinary object that getOwnPropertyDescriptor gives back. Here propertyDesc is the spec's descriptor Record, and obj is the new ordinary object.
1. If propertyDesc has a [[Value]] field, then
a. Perform CreateDataPropertyOrThrow(obj, "value", propertyDesc.[[Value]]).In plain words, line by line:
- Check whether the descriptor Record has a
[[Value]]field. - If it does, give the new object a property named
valuethat holds the same value.
So the spec field [[Value]] becomes the ordinary property value. The full operation in FromPropertyDescriptor does the same for every other field. The opposite operation, ToPropertyDescriptor, reads the settings object you pass to defineProperty.
The replay below runs the lesson's real describeOrder function.
A replay of the example code as it adds a property and reads its flags. It does not show the inside of the engine.
script
Object.defineProperty(order, "status", { value: "ready", enumerable: false });const description = Object.getOwnPropertyDescriptor(order, "status");console.log(description.value, description.writable);console.log(Object.keys(order).length);Line 2 adds status with the value "ready" and enumerable: false. Line 3 asks for its descriptor. Line 4 prints ready false: the value, then the writable flag. Line 5 prints 0, because no key is enumerable. The descriptor also says configurable is false. Back goes to an earlier step. It does not undo anything in your own page.
const order = {};Object.defineProperty(order, "status", { value: "ready", enumerable: true });console.log(Object.keys(order));statusreadytrueorder.status still holds ready. The enumerable flag is true, so Object.keys lists status.
Now try it yourself. The checkbox changes only the enumerable flag on line 2. When it is on, Object.keys lists status. When it is off, the list is empty, but order.status is still ready. Reset turns the flag back on.
This answers a common question when you fix bugs. A property can exist and still be missing from Object.keys.
Properties that hold a value or run a getter
There are two kinds of property:
- A data property holds a value. Its descriptor has
[[Value]]and[[Writable]]. - An accessor property runs a function each time you use it. Its descriptor has
[[Get]]and[[Set]]. A getter is the function that runs when you read the property. A setter is the function that runs when you write to it.
Both kinds have enumerable and configurable. One descriptor can't mix the two kinds. If you pass both value and get to defineProperty, it throws a TypeError.
const order = { price: 3 };Object.defineProperty(order, "total", { get() { return this.price; } });console.log(order.total);order.price = 4;console.log(order.total);Line 1 makes an order with a price of 3. Line 2 adds a getter called total. It returns this.price, the price of this order. Line 3 reads order.total, which runs the getter, and prints 3. Line 4 changes the price to 4. Line 5 runs the getter again and prints 4. The getter works out the total each time. No total is stored.
The table shows what each descriptor field is for. Read each row as: "this field belongs to this kind of property, and it controls this".
| Field | Which kind of property | What it controls |
|---|---|---|
value and writable | Data property | The value it holds, and whether = can change it. |
get and set | Accessor property | The functions that run when code reads or writes the property. |
enumerable | Both kinds | Whether the property shows up in lists of keys, like Object.keys. |
configurable | Both kinds | Whether the property can be deleted, or its flags changed. |
The writable flag decides whether = can change a data property. The next example uses Reflect.set(obj, name, value). It tries the same write as =, but it returns true or false instead of throwing. That way the result is the same in strict mode and in non-strict mode.
const cart = {};
Object.defineProperty(cart, "total", { value: 3 });
console.log(Object.getOwnPropertyDescriptor(cart, "total").writable);
console.log(Reflect.set(cart, "total", 4), cart.total);Line 1 makes an empty cart. Line 2 adds total with the value 3. Line 3 prints false, because writable was left out. Line 4 tries to write 4 and prints false 3. The write failed, and the value is still 3.
const todo = {};
Object.defineProperty(todo, "done", { value: false, configurable: true });
console.log(delete todo.done);
console.log("done" in todo);Line 1 makes an empty todo. Line 2 adds done with configurable: true. Line 3 deletes it and prints true, because the delete worked. Line 4 prints false, because done is gone. Without configurable: true, the delete would fail.
const user = { name: "Asha" };
Object.defineProperty(user, "password", { value: "secret" });
console.log(Object.keys(user).join(","));
console.log(Object.getOwnPropertyDescriptor(user, "password").enumerable);Line 1 makes a user with a name. Line 2 adds a password property and leaves enumerable out, so it is false. Line 3 prints only name. Line 4 prints false, the password's enumerable flag.
Hiding a key from Object.keys does not keep it secret. Any code can still read user.password or check its descriptor.
Lists keep order; Records name their fields
A List is a row of values in a fixed order. The spec uses Lists inside its steps. For example, the values you pass to a function are kept in a List. The spec writes a List with angle quotes, like « "tea", 3 ». A List is not a JavaScript Array. Your code never gets to see it.
function receipt(item, price) { console.log(item, price);}receipt("tea", 3);Line 1 starts a function, receipt, with two parameters: item, then price. Line 2 prints both. Line 4 passes "tea", then 3. The first value goes to the first parameter, so the output is tea 3. The spec keeps those two values in a List.
The box below quotes two sentences from the spec's List definition. They show how the spec writes a List.
"« 1, 2 » defines a List value that has two elements"
"A new empty List can be expressed as « »."In plain words, line by line:
« 1, 2 »is a List with two values: 1, then 2.« », with nothing inside, is an empty List.
The full text is in the List and Record definitions. It also says that the values passed to a function are kept in a List. The order matters: swap the two values in the call above, and item gets 3.
A Record is a set of named fields. The spec writes each field name in double square brackets, like [[Value]]. A Property Descriptor is one kind of Record. A JavaScript object with named properties looks a bit like a Record. The next example uses one only to compare. It is not a spec Record.
const result = { status: "ready", count: 3 };
console.log(result.status, result.count);Line 1 makes an ordinary object with two named properties, status and count. Line 2 prints ready 3. A spec Record also has named fields. But its fields can hold other spec values, and you can't put a Record in a JavaScript variable.
const label = { status: "ready" };
label.status = "sent";
console.log(label.status);Line 1 makes a real label object. Line 2 changes its status. Line 3 prints sent. This change happens to your object only. It does not change any spec Record that the spec used in earlier steps.
The table sums up four spec types. You'll meet the last two in the next sections. Read each row as: "this spec type does this job, and it is not the same as this".
| Spec type | What it does | What it is not |
|---|---|---|
| List | Keeps values in a fixed order, like the values passed to a function. | It is not a JavaScript Array. |
| Record | Holds named fields, like [[Value]] in a Property Descriptor. | It is not a JavaScript object. |
| Abstract Closure | Saves some spec steps, and the values they need, to run later. | It is not a function your code can call. |
| Data Block | Holds a fixed number of raw bytes. | It is not the ArrayBuffer object. The buffer is how your code reaches the bytes. |
Abstract Closures save values for later steps
An Abstract Closure is a small set of spec steps saved to run later, together with some values it keeps. The spec makes one in the middle of its steps. Later it calls it, the way you call a function. When the spec says the closure captures a name, the closure keeps the value that name has at that moment.
First, here is an ordinary JavaScript closure, only to compare. A closure is a function that keeps using variables from the place where it was made.
function makeTax(adjustment) { return (price) => price + adjustment;}const addTax = makeTax(2);console.log(addTax(3));Line 1 starts makeTax, which takes an adjustment. Line 2 returns a new arrow function that adds adjustment to a price. Line 4 calls makeTax(2) and saves the new function as addTax. Line 5 calls addTax(3) and prints 5.
addTax is a JavaScript function. It is not an Abstract Closure. No JavaScript code can get hold of an Abstract Closure.
The box below is a shortened copy of the example in the spec's Abstract Closure definition.
1. Let addend be 41.
2. Let closure be a new Abstract Closure with parameters (x) that captures addend.
3. Let value be closure(1).In plain words, line by line:
- Call the number 41
addend. - Make an Abstract Closure that takes one value,
x, and keepsaddend. In the spec, this step also says what the closure does: addxandaddend. That part is left out here. - Call the closure with 1, and call the result
value.
In the full Abstract Closure definition, value ends up as 42. The closure keeps the value that addend had when the closure was made.
This rule is only for Abstract Closures. A JavaScript closure keeps using the variable itself, so it sees later changes to that variable.
let price = 3;
const savedPrice = price;
const showSaved = () => savedPrice;
price = 4;
console.log(showSaved(), price);Line 1 gives price the value 3. Line 2 copies that number into savedPrice. Line 3 makes a small function that returns savedPrice. Line 4 changes price to 4. Line 5 prints 3 4: the saved copy is still 3.
The copy on line 2 acts like a spec capture: it keeps the value from that moment. It is only a comparison. It is not how the spec builds an Abstract Closure.
Data Blocks hold raw bytes
A byte is a small whole number from 0 to 255. A Data Block is the spec's name for a fixed-size row of bytes. Its bytes can change, but its size can't. A new Data Block starts with every byte set to 0.
Two JavaScript tools give your code access to bytes:
- An
ArrayBufferis a JavaScript object that owns a row of bytes. You can't read the bytes through it directly. - A typed array, like
Uint8Array, is a view. It lets your code read and write those bytes as numbers.
const buffer = new ArrayBuffer(3);const bytes = new Uint8Array(buffer);console.log(bytes.join(","));bytes[1] = 255;console.log(bytes.join(","));Line 1 makes a buffer with three bytes. Line 2 makes a Uint8Array view of those bytes. Line 3 prints 0,0,0. Line 4 writes 255 into position 1. That is the second byte, because positions start at 0. Line 5 prints 0,255,0.
The view is what your code works with. The Data Block is the spec's way to describe the bytes underneath.
bytes[1] = 255 changes only the middle byte.The box below comes from the spec's CreateByteDataBlock operation, which makes a new Data Block. Here size is how many bytes to make.
1. Let dataBlock be a new Data Block value consisting of size bytes.
2. Set all of the bytes of dataBlock to 0.
3. Return dataBlock.In plain words, line by line:
- Make a new Data Block with
sizebytes, and call itdataBlock. - Set every byte to 0.
- Give
dataBlockback.
Step 2 is why line 3 above printed 0,0,0. The full steps are in CreateByteDataBlock. A real engine can store bytes any way it likes. The spec only promises what your code sees: new bytes are 0.
A DataView is another kind of view. It can write numbers that need more than one byte. Its setUint16 method writes a whole number from 0 to 65535 into two bytes.
const buffer = new ArrayBuffer(2);
const view = new DataView(buffer);
view.setUint16(0, 258);
console.log(view.getUint8(0), view.getUint8(1));Line 1 makes a buffer with two bytes. Line 2 makes a DataView of it. Line 3 writes the number 258 into both bytes, starting at position 0. Line 4 reads each byte on its own and prints 1 2.
Why 1 and 2? Because 258 is 1 × 256 + 2. By default, setUint16 puts the bigger part first. This order is called big-endian. The Data Block only holds the bytes. The view decides how a bigger number is split into bytes.
The spec also has Shared Data Blocks. These are bytes that more than one agent can use at the same time. An agent is a separate place where JavaScript runs, like the main page and a worker. Shared bytes come with extra rules about who sees which change, and when. A plain ArrayBuffer, like the ones above, is not shared.
Using these words in real work
Start with something you can see go wrong. Say a theme setting works in your app, but it is missing from a list of keys. Before you change anything, check the property's descriptor. Is enumerable set to false? Knowing the spec's field names tells you which JavaScript tool can answer the question.
const settings = {};
Object.defineProperty(settings, "theme", { value: "light", enumerable: true });
console.log(Object.keys(settings).join(","));Line 1 makes an empty settings object. Line 2 adds a theme property with enumerable: true. Line 3 prints theme. With enumerable: false, the key would be missing from Object.keys. But settings.theme would still be "light".
Each spec type helps with a different kind of question:
- Completion Records help you follow a waiting
returnthrough afinallyblock. - Property Descriptors explain why a property can't be changed, deleted, or listed.
- Lists help you keep track of the order of the values passed to a function.
- Data Blocks help you tell the bytes apart from the views that read them.
None of them asks you to build a spec value yourself.
[[Type]]: THROW[[Target]]ofbreak outer;[[Writable]]: false[[Get]]- The values passed to a function, in order
- The bytes under an
ArrayBuffer
Put each clue in the group it belongs to. Then read why it fits there.
Common mistakes
It is easy to think every word in a spec step is a JavaScript object. It isn't. The spec describes what JavaScript must do. It does not say how an engine must store its helper values.
const todo = {};
console.log(Object.getOwnPropertyDescriptor(todo, "done"));Line 1 makes an empty todo. Line 2 asks for the descriptor of done, which doesn't exist, and prints undefined. That is a real JavaScript value that the tool gives back. It is not the spec's EMPTY.
- "Every abrupt completion is an error." A
returnis abrupt too, and it is not an error. Only THROW is about errors. - "The spec's descriptor is the object I get back." The spec's Record lives inside the spec.
getOwnPropertyDescriptorgives you a new ordinary object made from it. - "Not enumerable means private." The property is only missing from key lists. Any code can still read it.
- "EMPTY is undefined." EMPTY is the spec's word for "nothing here".
undefinedis a JavaScript value your code can use. - "Spec Lists are JavaScript Arrays." A List is a row of values inside the spec. Your code never sees it.
- "JavaScript closures save a copy of each value." Abstract Closures keep values. JavaScript closures keep using the variables themselves, so they see later changes.
The table puts side by side some words that are easy to mix up. Read each row as: "this word means this, and it is not the same as that".
| Spec word | What it means | What it is not |
|---|---|---|
| Completion Record | The spec's note about how a step finished, and with which value. | It is not the value a JavaScript function returns. |
| Property Descriptor | The spec's Record of one property's value and flags. | It is not the object getOwnPropertyDescriptor gives back. That object is a new one, made for your code. |
| EMPTY | The spec's word for "nothing here". | It is not the JavaScript value undefined. |
| Abstract Closure | Spec steps that keep the values names had when the closure was made. | It is not a JavaScript closure. A JavaScript closure keeps using the variables themselves. |
When you read a new spec step, sort each word into one of two groups. Is it a JavaScript value your code can see? Or is it a spec helper value that explains the step? Then try a small program to see the result for yourself.
Practice exercises
Guess each result before you run the code in the editor. Type your answer to check it. If you get stuck, open the hints one at a time. The exercises start with a return inside finally and end with the settings of a website.
The status function gets ready to return, then runs a finally block. Which single word does the console print? Say to yourself which return reaches the caller.
function status() { try { return "ready"; } finally { return "closed"; } }
console.log(status());function status() { try { return "ready"; } finally { return "closed"; } }
console.log(status());The return inside finally replaces the waiting return of "ready", so the code prints closed.
The code adds total to a cart with only a value. What does its descriptor say for enumerable? When you're done, compare it with a property made by cart.total = 3.
const cart = {};
Object.defineProperty(cart, "total", { value: 3 });
console.log(Object.getOwnPropertyDescriptor(cart, "total").enumerable);const cart = {};
Object.defineProperty(cart, "total", { value: 3 });
console.log(Object.getOwnPropertyDescriptor(cart, "total").enumerable);enumerable was left out when total was added, so the descriptor says false.
The receipt function takes an item and a price. Predict what it prints, including the colon. Which value goes to the first parameter?
function receipt(item, price) { return item + ":" + price; }
console.log(receipt("tea", 3));function receipt(item, price) { return item + ":" + price; }
console.log(receipt("tea", 3));The values go to item, then price, in order, so the function gives back tea:3.
This small JavaScript function uses adjustment from outside itself. What does the call print? Then say why running it does not give you a spec Abstract Closure.
const adjustment = 2;
const add = (price) => price + adjustment;
console.log(add(3));const adjustment = 2;
const add = (price) => price + adjustment;
console.log(add(3));The arrow function adds 2 to 3 and prints 5. It is an ordinary JavaScript closure, not a spec Abstract Closure.
A new buffer with two bytes gets a Uint8Array view. One byte changes. Type both bytes in the order they print. Which spec step says what new bytes start as?
const bytes = new Uint8Array(new ArrayBuffer(2));
bytes[0] = 255;
console.log(bytes.join(","));const bytes = new Uint8Array(new ArrayBuffer(2));
bytes[0] = 255;
console.log(bytes.join(","));The first byte becomes 255 and the second byte stays 0, so the output is 255,0.
A website lists its visible settings with Object.keys. Its theme setting was added with defineProperty. Which key does the list show? If that key went missing, which flag would you change?
const settings = {};
Object.defineProperty(settings, "theme", { value: "light", enumerable: true });
console.log(Object.keys(settings).join(","));const settings = {};
Object.defineProperty(settings, "theme", { value: "light", enumerable: true });
console.log(Object.keys(settings).join(","));theme is enumerable, so Object.keys lists it and the program prints theme.
Check your understanding
For each question, first decide what it is about: how code finishes, the flags of a property, or values in order. Then work out what the code prints by reading it line by line.
Question 1 of 8Which Completion Record type is not abrupt?
Choose an answer to see the explanation.
Question 2 of 8What does this finally example print?
Read the code, then predictfunction status() { try { return "ready"; } finally { return "closed"; } } console.log(status());Choose an answer to see the explanation.
Question 3 of 8What does
[[Target]]hold in a Completion Record?Choose an answer to see the explanation.
Question 4 of 8What does this descriptor example print?
Read the code, then predictconst cart = {}; Object.defineProperty(cart, "total", { value: 3 }); console.log(Object.getOwnPropertyDescriptor(cart, "total").writable);Choose an answer to see the explanation.
Question 5 of 8Can one descriptor have both
valueandget?Choose an answer to see the explanation.
Question 6 of 8What does the spec keep in a List when you call a function?
Choose an answer to see the explanation.
Question 7 of 8What does this code print for two new bytes?
Read the code, then predictconst bytes = new Uint8Array(new ArrayBuffer(2)); console.log(bytes.join(","));Choose an answer to see the explanation.
Question 8 of 8What does an Abstract Closure keep when the spec says it captures a name?
Choose an answer to see the explanation.
If you miss a question, go back to its small example above and change one value. This helps most with finally, the enumerable checkbox, and new bytes.
Key takeaways
The spec gives names to the work in the middle of each step, so its steps are exact. Your program sees only the results, never the spec's helper values.
- A Completion Record says how a step finished: NORMAL, BREAK, CONTINUE, RETURN, or THROW. It also holds a value and a target.
- Abrupt means the code jumps somewhere else. A
returnis abrupt, but it is not an error. - A Property Descriptor holds a property's value or getter and setter, plus its
writable,enumerable, andconfigurableflags. - Lists keep values in order. Records have named fields. Neither one is an Array or object your code can store.
- Abstract Closures keep values for later spec steps. Data Blocks are bytes that start at 0.
- When you read a spec step, check what it does with a small real program.
Remember the one-liner.
Spec types describe how JavaScript behaves; your code only works with the values that come out.
Coming next: The Reference type & this. That lesson shows how JavaScript remembers which object a method came from, so the method call knows its this. After that, Environment Records explain how JavaScript finds variable names, and Abstract Operations show how the spec's steps fit together.