The constructor describes the properties that are going to be on the object you create when you call new MyClass
. It’s just a function that returns an object.
This:
function Person(fullName) {
this.fullName = fullName;
}
Is the same as
class Person {
constructor(fullName) {
this.fullName = fullName;
}
}
Both of them, when you run new Person('Harold Human')
, will return an object that looks like:
{ fullName: 'Harold Human' }
You have a function that takes some arguments, and anything you assign to this.someProperty
will appear under the key someProperty
in the object you can create with the function.
You have a rogue extra line of code in your class that is a syntax error and will cause it to blow up. And the function takes two arguments, the property fullName
is going to need to join those two things together.
EDIT: I just read this back, and I mistyped at the end there, and missed out something, so for future reference for anyone looking at this thread
the property fullName
is going to need to join those two things together.
should have been
the method fullName
is going to need to join those two things together.
And I should have added what a method looks like:
The properties you add to the object can be functions, and in this case it is asking you to add a function that joins the first and last names together. The way you define them is like this:
class Person {
constructor(fullName) {
this.fullName = fullName;
}
// method
greet() {
return `Hello ${this.fulllName}`;
}
}
They are added in addition to the constructor function, and allow you to access/update properties defined on the object (like this.fullName
).