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"
1function Bird() {
2 this.name = "Albert";
3 this.color = "blue";
4 this.numLegs = 2;
5}
6/*
7This constructor defines a Bird object with properties name, color, and
8numLegs set to Albert, blue, and 2, respectively.
9Constructors follow a few conventions:
10-Constructors are defined with a capitalized name to distinguish them from
11other functions that are not constructors.
12
13-Constructors use the keyword this to set properties of the object they will
14create. Inside the constructor, this refers to the new object it will create.
15
16-Constructors define properties and behaviors instead of returning a value as
17other functions might.
18*/