Syntax ⧉ Copy class ClassName {
constructor ( params) { ... }
method () { ... }
} Example ⧉ Copy class Product {
constructor ( name, price) {
this . name = name;
this . price = price;
}
format () {
return ` ${ this . name } : $ ${ this . price . toFixed ( 2 )} ` ;
}
}
const item = new Product ( "Notebook" , 12.5 );
console . log ( item. format ()); Output "Notebook: $12.50"Note Classes are syntactic sugar over prototypes. They are NOT hoisted -- you must declare before use, unlike function declarations.
Syntax ⧉ Copy get propertyName () { ... }
set propertyName ( value) { ... } Example ⧉ Copy class Temperature {
#celsius;
constructor ( celsius) {
this . #celsius = celsius;
}
get fahrenheit () {
return this . #celsius * 9 / 5 + 32 ;
}
set fahrenheit ( f) {
this . #celsius = ( f - 32 ) * 5 / 9 ;
}
get celsius () { return this . #celsius; }
}
const temp = new Temperature ( 100 );
console . log ( temp. fahrenheit );
temp. fahrenheit = 32 ;
console . log ( temp. celsius ); Note Getters and setters look like regular properties from the outside. They are ideal for computed values, validation, and encapsulation.
getter setter get set computed property accessor
Static Methods and Properties Syntax ⧉ Copy class C {
static method () { ... }
static property = value;
} Example ⧉ Copy class MathUtils {
static PI = 3.14159265 ;
static clamp ( value, min, max) {
return Math . min ( Math . max ( value, min), max);
}
static lerp ( a, b, t) {
return a + ( b - a) * t;
}
}
console . log ( MathUtils . clamp ( 150 , 0 , 100 ));
console . log ( MathUtils . lerp ( 0 , 50 , 0.5 )); Note Static members belong to the class itself, not instances. Access them via ClassName.method(), not instance.method().
Syntax ⧉ Copy class Child extends Parent {
constructor ( params) {
super ( parentParams);
}
} Example ⧉ Copy class Shape {
constructor ( color) {
this . color = color;
}
describe () {
return `A ${ this . color } shape` ;
}
}
class Circle extends Shape {
constructor ( color, radius) {
super ( color);
this . radius = radius;
}
get area () {
return Math . PI * this . radius ** 2 ;
}
describe () {
return `A ${ this . color } circle (r= ${ this . radius } )` ;
}
}
const c = new Circle ( "blue" , 5 );
console . log ( c. describe ());
console . log ( c. area . toFixed ( 2 )); Output "A blue circle (r=5)"
"78.54"Note super() must be called in the child constructor before accessing this. Methods can be overridden. Use super.method() to call the parent version.
Private Fields and Methods Syntax ⧉ Copy class C {
#privateField = value;
#privateMethod () { ... }
} Example ⧉ Copy class BankAccount {
#balance = 0 ;
constructor ( initial) {
this . #balance = initial;
}
deposit ( amount) {
this . #validateAmount ( amount);
this . #balance += amount;
}
get balance () {
return this . #balance;
}
#validateAmount ( amount) {
if ( amount <= 0 ) throw new Error ( "Amount must be positive" );
}
}
const acct = new BankAccount ( 100 );
acct. deposit ( 50 );
console . log ( acct. balance );
Note Private fields/methods use the # prefix. They are truly private -- not accessible outside the class, even by subclasses. instanceof checks still work.
Static Initialization Block Syntax ⧉ Copy class C {
static { }
} Example ⧉ Copy class Config {
static defaults;
static env;
static {
Config . env = typeof window !== "undefined" ? "browser" : "node" ;
Config . defaults = Config . env === "browser"
? { timeout: 10000 , retries: 2 }
: { timeout: 30000 , retries: 5 };
}
}
console . log ( Config . env );
console . log ( Config . defaults ); Note Static blocks run once when the class is evaluated. Useful for complex static initialization that cannot be done in a single expression.
static block static initialization class static setup
instanceof and Type Checking Syntax ⧉ Copy object instanceof ClassName Example ⧉ Copy class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
const pet = new Dog ();
console . log ( pet instanceof Dog );
console . log ( pet instanceof Animal );
console . log ( pet instanceof Cat ); Note Checks the prototype chain. Can be unreliable across iframes or realms. For built-in types, prefer Array.isArray() over instanceof Array.
Syntax ⧉ Copy const Mixin = ( Base ) => class extends Base { ... }; Example ⧉ Copy const Serializable = ( Base ) => class extends Base {
toJSON () {
return JSON . stringify ({ ... this });
}
};
const Timestamped = ( Base ) => class extends Base {
constructor (... args ) {
super (... args );
this . createdAt = new Date ();
}
};
class User extends Timestamped ( Serializable ( Object )) {
constructor ( name) {
super ();
this . name = name;
}
}
const user = new User ( "Kai" );
console . log ( user. toJSON ()); Note JavaScript has single inheritance only. Mixins simulate multiple inheritance by composing class factories. Order matters -- later mixins override earlier ones.
mixin multiple inheritance compose classes class composition