Understanding Constructors


class Surgeon{
constructor(name, department){
this.name = name;
this.department = department;

The instructions are

Inside the Surgeon constructor(), create name and department properties and set them equal to your input parameters.

I wanted to know which end are my parameters and which end is are my properties.
If I’m correct
this.department (These are my properties).

My other question. Hopefully, I can explain this properly.
When I was creating objects the syntax is:

let myObject ={
name: ‘blah’,
color: ‘blah’

I could then call back to name and color but with classes, I never created these keys.
How is it that I’m referring to name and department using this when name and department were never created.



yes, correct, and your parameters are name and department, which we can see here:

constructor(name, department)

just like functions when we define a function, the parameters are provided at method declaration

yes, objects are different from classes.

just like function parameters get there values from arguments at function call, the constructor parameters get there value from when you create instance of class


Perfect thanks for the help! That should be all.