Runtime foundations
JavaScript Is the Runtime
Use runnable examples to understand shared objects, remembered variables, and module loading.
Does a variable hold an object or a reference to it?
Primitive values are compared by value. An object variable points to an object's identity. Assigning it to another variable does not copy the object, and object spread copies only one level.
This causes subtle AI-generated bugs: a factory reuses one default tags array, so tasks unexpectedly change each other.
const base = { options: { retries: 2 } };
const copy = { ...base };
copy.options.retries = 9;
console.log(base.options.retries); // 9Why do two tasks share tags?
Predict and run the output. Then give each task its own tags array.
运行后显示诊断。
等待执行。
Need a hint?
Object spread copies one level; a nested array may still be shared.
Closures remember surrounding variables; this depends on the call
A closure keeps access to variables around it. this is different: extracting a method and calling it another way can change this.
Prefer explicit parameters and small closures in backend code. Use classes when you really need an object lifecycle.
- Arrow functions capture the surrounding this
- A normal function gets this from the call site
- Shared mutable closure state needs a clear owner
Importing runs code, and throw can throw any value
An ES module runs its top-level code the first time it loads. Opening connections or timers during import makes tests depend on import order.
JavaScript can throw a string, object, or null, so keep catch values as unknown and check them before reading message.
Build one synchronous review task without a framework
Create ReviewJob, a factory, and a tiny CLI. Keeping the core independent from NestJS, databases, and model SDKs makes later changes safer.
- Each job gets independent state
- The entry point parses arguments
- The CLI decides how errors are displayed
Finish with evidence, not a feeling
After the Playground, run this check from the project root. It never contacts a model.
npm run verify:lesson -- M01