+ 2
Whatâs wrong with this code not printing?
const Person1= new Person("John", "Kennedy", "3-6-1895"); const Person2= new Person("Mary", "Kennedy", "3-6-1995"); class Person{ constructor Person =(firstName, lastName, dob){ this.firstName =firstName; this.lastNsme =lastName; this.bod= new Date(dob); } getBirthYear{ return this.bod.getBirthYear; } getFullName{ return ${this.firstName} ${this.lastName} } } document.write(Person2.getFullName()); document.write(Person1);
3 Answers
+ 6
//The full code simplified and should work mowđđđ
//And you made a typo in the code in this.lastName, yiy write this.lastNsme by accidentđđ
class Person{
constructor (firstName, lastName, dob){
this.firstName =firstName;
this.lastName =lastName;
this.bod= new Date(dob);
}
getBirthYear(){
return this.bod.getBirthYear;
}
getFullName(){
return this.firstName+" "+this.lastName
}
}
const Person1= new Person("John", "Kennedy", "3-6-1895");
const Person2= new Person("Mary", "Kennedy", "3-6-1995");
document.write(Person2.getFullName());
document.write(Person1);
+ 2
have seen the mistake. thanks buddy
+ 1
1. You should put `backticks` around the ${this.firstName} ${this.lastName} bit
2. Unlike other programming languages, JS class constructors just need the constructor keyword:
class Person {
constructor(firstName, lastName, dob) {
// Do something
}
}
Hope this helps.