class and object in javascript code example

Example 1: javascript create class

class Person {
 constructor(name, age) {
   this.name = name;
   this.age = age;
 }
  present = () => { //or present(){
  	console.log(`Hi! i'm ${this.name} and i'm ${this.age} years old`) 
  }
}
let me = new Person("tsuy", 15);
me.present();
// -> Hi! i'm tsuy and i'm 15 years old.

Example 2: javascript class

// Improved formatting of Spotted Tailed Quoll's answer
class Person {
	constructor(name, age) {
		this.name = name;
		this.age = age;
	}
	introduction() {
		return `My name is ${name} and I am ${age} years old!`;
	}
}

let john = new Person("John Smith", 18);
console.log(john.introduction());

Example 3: how to create object in javascript with class

let myCar1 = new Car("Ford", 2014);

let myCar2 = new Car("Audi", 2019);

Example 4: how to create object in javascript with class

class Car {

   constructor(name, year) {

    this.name = name;

    this.year = year;

  }

}

Example 5: javascript classes

let Person = class {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

Example 6: class in javascript

class Car {
    constructor(name, year) {
        this.name = name;
        this.year = year;
    }
    age(x) {
        return x - this.year;
    }
}

let date = new Date();
let year = date.getFullYear();

let myCar = new Car("Ford", 2014);
document.getElementById("class1").innerHTML = "My car is " + myCar.age(year) + " years old.";