JS: typeof Class

By Xah Lee. Date: . Last updated: .

Class is a special function object. In many ways, it's same as function defined via the keyword function .

Typeof on class

like function, typeof on class is "function".

console.assert(typeof (class {}) === "function");

Parent of class

like function, parent of class is Function.prototype.

console.assert(Reflect.getPrototypeOf(class {}) === Function.prototype);

Properties of class

like function, it has properties {length, name, prototype}.

console.log(Reflect.ownKeys(class Cc {}));
// [ "length", "name", "prototype" ]

The property key "prototype"

like function, the value of the property key "prototype", is a new object object with a property key "constructor", by default. 〔see Property Key "prototype"

class Xcc {}

// like function, it has a property key "prototype"
console.log(Object.hasOwn(Xcc, "prototype"));

// like function, the value of this property key "prototype", is a object with property key "constructor"
console.log(
 Reflect.ownKeys(Xcc.prototype),
);
// [ "constructor" ];

Value of property key "constructor"

like function, the value of property key "constructor", is the class function itself

class Cc {}

// the value of the property key "constructor", is Cc itself
console.log(Cc.prototype.constructor === Cc);

However, class function must be called with keyword new. It cannot be called like a function by itself.

JavaScript. Constructor, Class