Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Initialization
- Giving a variable its first value. Startup values are initialized before or during the program’s first use of them.
- Lazy initialization
- Building a value the first time it is read, then reusing it.
- Initialization order
- The order in which a language runs startup initializers.
- Temporal dead zone
- The period before a let or const declaration runs, when reading it throws a ReferenceError.
Dependencies and readiness are different questions
A dependency lets code refer to another unit’s names or values. Initialization establishes the values that running code will use. These are related operations with different language rules: finding a name does not by itself establish that every piece of setup on which its value depends has completed.
Eager initialization performs setup before a later use needs it; lazy initialization defers work until a use requests it. Retaining that result avoids rebuilding it, but also retains the inputs used at construction. For a derived table, ask both when its inputs are ready and how long the resulting table remains valid. The examples isolate these decisions using each language’s startup and first-use mechanisms.
01Order
Which startup value is ready first?
A shipping-rate helper is moving out of the checkout startup code. It must still quote from the configured base rates, and merchants can override zones before quotes begin. Which work can happen when a dependency loads, and which work must wait until those settings are complete?
Start with the helper’s smallest dependency: a surcharge that is 20% of a 500-cent base rate. Both values are fixed for this probe, but surcharge is declared first. If moving the setup changes when it runs, can the surcharge still see a ready base rate? These single-file programs isolate that question; they do not trace a real multi-file loader.
For the synchronous module body shown here, statements run in source order. A const binding cannot be read before its declaration initializes it. Importing a helper can evaluate that helper’s top-level work; it does not wait for later startup statements in the importer.
// A shipping-rate service's two startup values, declared in the opposite order to their dependency.
function build(name: string, value: () => number): number {
console.log(`making ${name}`);
return value();
}
// Cents per parcel: the surcharge is 20% of the base rate.
const surcharge = build("surcharge", () => (baseRate * 20) / 100);
const baseRate = build("base rate", () => 500);
console.log(`main: surcharge = ${surcharge}`);
npx tsc main.ts --noEmit --strict && node main.ts
Put each example in the shown file and run the command. The TypeScript command checks with tsc first; the first example passes the checker and fails only when it runs.
02Once
How do you build something at first use?
The helper also owns a rate table shared by successive quotes. Recomputing it at every lookup would repeat setup; constructing it while loading the helper would run setup even when no quote needs it. First isolate how to build and retain a table on demand. This fixed-rate probe has no merchant overrides yet.
A module-level variable holds the table between calls. The synchronous lookup builds it when that variable is undefined. This check is not a lock for asynchronous construction or shared workers.
// A rate table built the first time a quote needs it, then reused.
let table: Map<string, number> | undefined;
function rates(): Map<string, number> {
if (table === undefined) {
console.log("building the table");
table = new Map([["A", 250]]);
}
return table;
}
console.log("main starts");
console.log(rates().get("A"));
console.log(rates().get("A"));
04Practice
Finish the shipping-rate service
Complete the helper in a reduced shipping-rate process. Initialize the built-in table through its supplied row builder, make express rates use ready inputs, then accept merchant overrides without repeating the derived-table build for every quote. All zone lines precede every quote, and this process supports no settings changes after quoting begins. The supplied rates produce whole-cent express quotes; rounding policy is outside this startup probe. Each level supplies the parts that already work.
The shipping-rate service
Prepare built-in rates, respect native initialization, then cache the completed configuration on demand.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?