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".

typeof (class {}) === "function"

Parent of class

like function, parent of class is Function.prototype.

Reflect.getPrototypeOf(class {}) === Function.prototype

Properties of class

like function, it has properties { length, name, prototype}. (the property name is new in ES2015.)

// get properties of function and a class
const propsF = JSON.stringify(Reflect.ownKeys(function Ff() {}));
const propsC = JSON.stringify(Reflect.ownKeys(class Cc {}));

console.log(propsF === propsC);

console.log(propsC === `["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 Cc {}

// like function, it has a property key "prototype"
console.log(Cc.hasOwnProperty("prototype"));

// like function, the value of this property key "prototype", is a object with property key "constructor"
console.log(
  Reflect.ownKeys(Cc.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