JS: Define a Class Without class

By Xah Lee. Date: . Last updated: .

This page shows you how to define a class without keyword class, without keyword new, without keyword this. without keyword function

Using Keyword class

Suppose we want to define a class MyClass, with a constructor MyClass(x) that adds a property key xprop with value x, and one prototype method method1, and one static method staticm1.

// Using Keyword class

class MyClass {
  constructor(x) {
    this.xprop = x;
  }

  static staticm1(x) {
    return "staticm1 " + x.toString();
  }

  method1(x) {
    return "method1 " + x.toString();
  }
}

// HHHH------------------------------

console.log(MyClass.staticm1(3) === "staticm1 3");

const myObj = new MyClass(4);

console.log(myObj.hasOwnProperty("xprop"));
console.log(myObj.xprop === 4);

console.log(myObj.method1(2) === "method1 2");

console.log(Reflect.getPrototypeOf(myObj) === MyClass.prototype);
console.log(myObj.constructor === MyClass);

Without Keyword class

// define a class without Keyword class

function MyClass(x) {
  this.xprop = x;
}

MyClass.staticm1 = function (x) {
  return "staticm1 " + x.toString();
};

MyClass.prototype.method1 = function (x) {
  return "method1 " + x.toString();
};

// HHHH------------------------------

console.log(MyClass.staticm1(3) === "staticm1 3");

const myObj = new MyClass(4);

console.log(myObj.hasOwnProperty("xprop"));
console.log(myObj.xprop === 4);

console.log(myObj.method1(2) === "method1 2");

console.log(Reflect.getPrototypeOf(myObj) === MyClass.prototype);
console.log(myObj.constructor === MyClass);

Without Keywords class, new, this

// define a class without Keyword class new this

const MyClass = (x) => {
  const resultObj = Object.create(MyClass.prototype, {});
  resultObj.xprop = x;
  return resultObj;
};

MyClass.staticm1 = function (x) {
  return "staticm1 " + x.toString();
};

MyClass.prototype = {
  method1: function (x) {
    return "method1 " + x.toString();
  },
  constructor: MyClass,
};

// HHHH------------------------------

console.log(MyClass.staticm1(3) === "staticm1 3");

const myObj = MyClass(4);

console.log(myObj.hasOwnProperty("xprop"));
console.log(myObj.xprop === 4);

console.log(myObj.method1(2) === "method1 2");

console.log(Reflect.getPrototypeOf(myObj) === MyClass.prototype);
console.log(myObj.constructor === MyClass);

Without Keywords class, new, this, function

// define a class without Keyword class new this function

const MyClass = (x) => {
  const resultObj = Object.create(MyClass.prototype, {});
  resultObj.xprop = x;
  return resultObj;
};

MyClass.staticm1 = (x) => {
  return "staticm1 " + x.toString();
};

MyClass.prototype = {
  method1: ((x) => {
    return "method1 " + x.toString();
  }),
  constructor: MyClass,
};

// HHHH------------------------------

console.log(MyClass.staticm1(3) === "staticm1 3");

const myObj = MyClass(4);

console.log(myObj.hasOwnProperty("xprop"));
console.log(myObj.xprop === 4);

console.log(myObj.method1(2) === "method1 2");

console.log(Reflect.getPrototypeOf(myObj) === MyClass.prototype);
console.log(myObj.constructor === MyClass);

JavaScript. Constructor, Class