1function ClassMates(name,age){
2 this.name=name;
3 this.age=age;
4 this.displayInfo=function(){
5 return this.name + "is " + this.age + "year's old!";
6 }
7}
8
9let classmate1 = new ClassMates("Mike Will", 15);
10classmate.displayInfo(); // "Mike Will is 15 year's old!"
1class Person {
2 constructor(name) {
3 this.name = name;
4 }
5 introduce() {
6 console.log(`Hello, my name is ${this.name}`);
7 }
8}
1A constructor is a function that creates an instance of a class
2 which is typically called an “object”. In JavaScript, a constructor gets
3 called when you declare an object using the new keyword.
4 The purpose of a constructor is to create an object and set values if
5 there are any object properties present.
6
1function Person(first, last, age, eye) {
2 this.firstName = first;
3 this.lastName = last;
4 this.age = age;
5 this.eyeColor = eye;
6}
1function Car(make, model, year) {
2 this.make = make;
3 this.model = model;
4 this.year = year;
5}
6
7var car1 = new Car('Eagle', 'Talon TSi', 1993);
8
9console.log(car1.make);
10// expected output: "Eagle"
1// constructor function
2function Person () {
3 this.name = 'John',
4 this.age = 23
5}
6
7// create an object
8const person = new Person();