TSTypeScript · First PrinciplesBuild judgment for vibe coding
0%
M01 1 hr 45 min CodePilot P0

Runtime foundations

JavaScript Is the Runtime

Use runnable examples to understand shared objects, remembered variables, and module loading.

By the end, you can:Predict references and shallow copies;Explain closures, this, and module side effects;Safely handle any thrown value
01
Start here

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.

TypeScript
const base = { options: { retries: 2 } };
const copy = { ...base };
copy.options.retries = 9;
console.log(base.options.retries); // 9
TypeScript Lab

Why do two tasks share tags?

Predict and run the output. Then give each task its own tags array.

正在加载 TypeScript 编辑器…
Diagnostics 0

运行后显示诊断。

Output

等待执行。

Need a hint?

Object spread copies one level; a nested array may still be shared.

02
Where behavior comes from

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
03
Module boundary

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.

04
CodePilot P0

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
LESSON CHECK

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