Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 1.06 KB

README.md

File metadata and controls

37 lines (24 loc) · 1.06 KB

44. Classes and prototypes

Understand shared behavior and object construction.

Learn

  • class
  • constructor
  • prototype
  • this

How it works

Classes provide syntax for constructors and prototype methods. Methods are shared through the prototype, while instance fields belong to each object. this depends on how a normal function is called, so methods passed as callbacks may lose their expected receiver. Prefer composition when inheritance adds unnecessary coupling.

Common mistake

Copying a method into a variable can change this and cause a runtime error.

Guided challenge

Create a Timer class with start and stop; explain which data is per instance and which method is shared.

Run the example

Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.

Completion check

  • Explain the core idea in your own words without reading the code.
  • Change one input and predict the output before running it.
  • Test one boundary or failure case, then record what you changed.