classes and objects in javascript code example
Example 1: javascript classes
class MyClass {
constructor(FirstProperty, SecondProperty, etcetera) {
this.firstProperty = FirstProperty;
this.secondProperty = SecondProperty;
}
method(Parameters) {
}
get getBothValues()
{
return [firstProperty, secondProperty];
}
}
Example 2: how to create object in javascript with class
let myCar1 = new Car("Ford", 2014);
let myCar2 = new Car("Audi", 2019);
Example 3: how to create object in javascript with class
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
Example 4: javascript class
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area);