1class ClassMates{
2 constructor(name,age){
3 this.name=name;
4 this.age=age;
5 }
6 displayInfo(){
7 return this.name + "is " + this.age + " years old!";
8 }
9}
10
11let classmate = new ClassMates("Mike Will",15);
12classmate.displayInfo(); // result: Mike Will is 15 years old!
1class Person {
2 constructor(name, age) {
3 this.name = name;
4 this.age = age;
5 }
6 present = () => { //or present(){
7 console.log(`Hi! i'm ${this.name} and i'm ${this.age} years old`)
8 }
9}
10let me = new Person("tsuy", 15);
11me.present();
12// -> Hi! i'm tsuy and i'm 15 years old.
1class Person {
2 constructor(name, age) {
3 this.name = name;
4 this.age = age;
5 }
6 displayInfo() {
7 return this.name + ' is' + this.age + " years old";
8 }
9}
10
11const Anthony = new Person('Anthony', 32);
1// Improved formatting of Spotted Tailed Quoll's answer
2class Person {
3 constructor(name, age) {
4 this.name = name;
5 this.age = age;
6 }
7 introduction() {
8 return `My name is ${name} and I am ${age} years old!`;
9 }
10}
11
12let john = new Person("John Smith", 18);
13console.log(john.introduction());
1class Car {
2 constructor(name, year) {
3 this.name = name;
4 this.year = year;
5 }
6 age(x) {
7 return x - this.year;
8 }
9}
10
11let date = new Date();
12let year = date.getFullYear();
13
14let myCar = new Car("Ford", 2014);
15document.getElementById("class1").innerHTML = "My car is " + myCar.age(year) + " years old.";