create a class js code example
Example 1: javascript class
class ClassMates{
constructor(name,age){
this.name=name;
this.age=age;
}
displayInfo(){
return this.name + "is " + this.age + " years old!";
}
}
let classmate = new ClassMates("Mike Will",15);
classmate.displayInfo();
Example 2: js class
class Car {
constructor(brand, speed) {
this.brand = brand
this.speed = speed
}
speedUp() {
this.speed += 5
console.log(`The ${this.brand} is travelling at ${this.speed} mph`)
}
slowDown() {
this.speed -= 5
console.log(`The ${this.brand} is travelling at ${this.speed} mph`)
}
}
const redCar = new Car('toyota', 0)
redCar.speedUp()
redCar.slowDown()
Example 3: javascript class
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
let Rectangle = class Rectangle5 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
Example 4: javascript classes
let Person = class {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}