create object with object function javascript code example
Example 1: how to create object in javascript with class
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
Example 2: make an object javascript
class ObjectLayout {
constructor() {
this.firstName = "Larry"; //property
}
sayHi() { // Method
return `Hello from ${this.firstName}`;
}
}
const object = new ObjectLayout();
// object.firstName is Larry;
// object.sayHi() gives "Hello from Larry"