This course builds on JavaScript intermediate. You should already be comfortable with closures, modules, promises, async/await, AbortController, injected dependencies and basic assertions. Revisit async coordination, cancellation and integration tests if needed. No TypeScript is required.
We will design a small task-pool library. Its callers need a precise contract about concurrency, result order and failure. Before that, study language behavior that makes reusable objects and protocols work.
Property lookup first checks an object's own properties, then follows its prototype chain. Object.create(base) makes a new object whose prototype is base. Assigning an ordinary writable property on the child usually creates an own property that shadows the inherited one; deleting that own property exposes the inherited value again. Object.hasOwn distinguishes ownership from lookup. Avoid modifying built-in prototypes: it changes shared behavior outside your library.
Predict, run, and explain
Write the output and trace the important state changes before checking it. Then run the program. Each editor is independent. Async examples end with main() so this editor waits for their work.
const base = { label: "shared" };
const child = Object.create(base);
console.log(child.label, Object.hasOwn(child, "label"));
child.label = "local";
console.log(child.label, base.label);
delete child.label;
console.log(child.label);Check your prediction
shared false
local shared
sharedTrace lookup and ownership separately
A visible property need not be owned by the object you are inspecting. Lookup follows the prototype chain, while Object.hasOwn asks about the current object only. A default inherited from a prototype can therefore affect behavior without appearing among the object’s own keys.
Assignment to an ordinary writable inherited property creates a local shadow. Mutating an inherited object is different: you are following a shared reference and editing its contents. Draw the lookup chain and the reference graph separately before predicting a nested update.
const base = { limit: 2 };
const child = Object.create(base);
console.log(child.limit);
console.log(Object.keys(child).length);
child.limit = 4;
console.log(Object.keys(child).join(","));Check your prediction
2
0
limitThe first read finds the inherited default. Only after assignment does child own limit. Enumeration and property access answer different questions.
const base = { labels: ["shared"] };
const child = Object.create(base);
child.labels.push("changed");
console.log(base.labels.join(","));
child.labels = ["local"];
console.log(base.labels.join(","), child.labels.join(","));Check your prediction
shared,changed
shared,changed localPush changes the shared array found through lookup. Replacing child.labels later creates a local property but does not undo the mutation already made to the shared array.
Try the rule on new inputs
Test read, shadow, delete and nested mutation as separate operations. Explain why a library should not store mutable per-instance state in shared defaults, even when simple numeric defaults behave as expected.
Trace the contract before building
Separate lookup from ownership. Reading child.title may find an inherited property even though the child has no own title. Assigning a normal writable property on the child can shadow that value without editing the prototype. Deleting the shadow then reveals the inherited property again.
Trace the lookup path after every operation and use Object.hasOwn when the question is ownership. Do not infer ownership from a successful property read. This distinction prepares the shared-method behavior of classes, while the capstone still needs no modification of built-in prototypes or elaborate inheritance hierarchy.
Your turn
An inherited setter
The examples above assigned to plain inherited properties, which creates a shadow. An inherited accessor behaves differently. Write defaults with a limit getter and setter: the getter returns the receiver's own chosenLimit if it has one, else 2; the setter throws a RangeError unless the value is a positive integer, then stores it as chosenLimit on the receiver. Create two children with Object.create(defaults), set settings.limit = 4, then try settings.limit = 0. Print:
4 2 2
false true
RangeError 4const defaults = {
// Add get limit() and set limit(value).
};
const settings = Object.create(defaults);
const other = Object.create(defaults);
settings.limit = 4;
console.log(settings.limit, other.limit, defaults.limit);
console.log(Object.hasOwn(settings, "limit"), Object.hasOwn(settings, "chosenLimit"));
try {
settings.limit = 0;
} catch (error) {
console.log(error.name, settings.limit);
}Hint 1: the idea
Assigning to a property that the prototype chain defines with a setter calls that setter, with this set to the object you assigned through. No own limit is created, so where the value lives is entirely the setter's decision.
Hint 2: a first step
Write only the getter first, returning 2, and run the starter. The assignment settings.limit = 4 now fails. An inherited accessor with no setter blocks assignment rather than letting it make a shadow. Strict code (modules, classes, the course's tests) throws TypeError: Cannot set property limit of #<Object> which has only a getter. This editor's classic script ignores the assignment silently and prints 2 2 2 and false false. Either way limit is stuck at 2 until you add the setter.
Show one solution
const defaults = {
get limit() {
return Object.hasOwn(this, "chosenLimit") ? this.chosenLimit : 2;
},
set limit(value) {
if (!Number.isInteger(value) || value < 1) {
throw new RangeError("limit must be a positive integer");
}
this.chosenLimit = value;
}
};
const settings = Object.create(defaults);
const other = Object.create(defaults);
settings.limit = 4;
console.log(settings.limit, other.limit, defaults.limit);
console.log(Object.hasOwn(settings, "limit"), Object.hasOwn(settings, "chosenLimit"));
try {
settings.limit = 0;
} catch (error) {
console.log(error.name, settings.limit);
}4 2 2
false true
RangeError 4settings.limit = 4 found the setter on defaults and ran it with this === settings, so chosenLimit became an own property of settings, while limit itself stayed inherited: hence false true. other and defaults have no chosenLimit, so their getters fall back to 2. Writing defaults.chosenLimit = value inside the setter, the plausible mistake, would print 4 4 4: one child's choice leaking to every object that shares the prototype. The RangeError is thrown before any assignment, so the rejected 0 leaves 4 in place. Compare the getter-only first step: with no setter on the chain, the assignment could not run any code and could not make a shadow either, so it failed (a TypeError in strict code, silently in sloppy code). That is the difference from the plain data properties earlier in the lesson, where assignment simply creates an own copy.
Change it and test it
Add a nested array to defaults and push through settings. Explain why both references see the change: lookup did not copy the array. Then give each child its own array and verify independence. A library option snapshot should not accidentally mutate shared defaults.
Completion is saved in this browser only. It does not measure mastery.