Sub topic 1
Declaring a Class
A class is a template for creating objects that share the same structure and behavior. JavaScript gives you two ways to define one: a class declaration and a class expression. This chapter covers both forms and how the class name is bound in each.
Prerequisites
This chapter assumes you already know:
- JavaScript variables, functions, and arrow functions
- Objects and property access
- How to run code in a browser console or Node.js
Class Declarations
A class declaration starts with the class keyword, followed by a name and a body in braces. By convention, class names use PascalCase.
class Greeter {
greet(name) {
return `Hello, ${name}!`;
}
}
const g = new Greeter();
console.log(g.greet('Ada')); // 'Hello, Ada!'The declaration creates a binding named Greeter in the enclosing scope, the same way let does. You create objects from it with new, which a later chapter covers in detail.
The class body holds methods and fields. You don't put commas between members, which is different from an object literal:
class Temperature {
toCelsius(f) {
return (f - 32) * 5 / 9;
}
toFahrenheit(c) {
return c * 9 / 5 + 32;
}
}
console.log(new Temperature().toCelsius(212)); // 100Class Expressions
A class can also appear anywhere an expression is allowed. You can assign it to a variable, pass it to a function, or return it.
const Logger = class {
log(msg) {
return `[log] ${msg}`;
}
};
console.log(new Logger().log('started')); // '[log] started'
console.log(Logger.name); // 'Logger'This class has no name of its own. JavaScript infers Logger as its name property from the variable it is assigned to.
Named Class Expressions
A class expression can have its own name. That name is visible only inside the class body, so the class can refer to itself even if the outer variable gets reassigned.
const Shape = class Polygon {
describe() {
return `I am a ${Polygon.name}`;
}
};
console.log(new Shape().describe()); // 'I am a Polygon'
console.log(Shape.name); // 'Polygon'
console.log(typeof Polygon); // 'undefined'Classes Are Values
Both forms produce a value you can pass around like any other value. This is what makes factories and mixins possible, which later chapters build on.
function build(Cls) {
return new Cls();
}
class Box {
open() {
return 'opened';
}
}
console.log(build(Box).open()); // 'opened'Choosing Between the Forms
| Form | Name binding | Typical use |
|---|---|---|
| Declaration | Enclosing scope | Most application classes |
| Anonymous expression | Inferred from variable | Inline or conditional definitions |
| Named expression | Inside body only | Self-reference that doesn't depend on the outer variable |
Exercise
Given const A = class B {};, what do A.name and typeof B print when logged outside the class?
Show answer
A.name is 'B', because the inner name takes priority over the inferred one. typeof B is 'undefined', because the name B is bound only inside the class body.
Exercise
Rewrite class Clock { now() { return Date.now(); } } as an anonymous class expression stored in a constant named Clock.
Show answer
const Clock = class { now() { return Date.now(); } }; Here Clock.name is still 'Clock' through name inference.