create function constructor javascript es6 code example

Example 1: javascript constructor function

function ClassMates(name,age){
  this.name=name;
  this.age=age;
  this.displayInfo=function(){
    return this.name + "is " + this.age + "year's old!";
  }
}

let classmate1 = new ClassMates("Mike Will", 15);
classmate.displayInfo(); // "Mike Will is 15 year's old!"

Example 2: JavaScript constructor function

// constructor function
function Person () {
    this.name = 'John',
    this.age = 23
}

// create an object
const person = new Person();