create class es5 in javascript

To create a class in ES5, you need to use a function constructor and add properties and methods to its prototype. Here's an example:

index.tsx
// Define a constructor function
function Car(name, year) {
  this.name = name;
  this.year = year;
}

// Add methods to the Car prototype
Car.prototype.start = function() {
  console.log(`Starting ${this.name}...`);
};

Car.prototype.stop = function() {
  console.log(`${this.name} stopped.`);
};

// Create an instance of Car
const myCar = new Car('Honda', 2021);

// Call the methods of myCar
myCar.start(); // Starting Honda...
myCar.stop();  // Honda stopped.
465 chars
22 lines

In this example, we define a Car constructor function that takes name and year parameters and assigns them to this.name and this.year properties. We then add start and stop methods to the Car.prototype. Finally, we create an instance of Car and call its start and stop methods.

gistlibby LogSnag