Getter and setters help

I watched this video : https://www.youtube.com/watch?v=ae5sqxUmL70 on the topic, which seems to be explained pretty clearly and helped me understand the purpose of getters and setters.

However when I tried to write the same code in the console, it comes back with an error, screenshot below. I’m confused because the code is exactly the same as in the tutorial video - have I missed something?

What was the original code? it is hard to find your reference through video. It is time consuming

Sorry, here is the code the tutorial has:

It appears in the video at 8:47

Hi @chunzg,

I copied your code to console and it worked. You are getting this error because you have declared this person already.

1 Like

Hello there,

I only get that error, when I try to redefine person:
image

Seeing as person is defined as a constant, you cannot re-assign anything to that variable name.

Just as a word of warning: It is quite bad code doing this: ${person.firstName} ${person.lastName}

It should be: ${this.firstName} ${this.lastName}

Hope this helps

1 Like

Thank you for this tip!

You’re right, thanks. I opened a new incognito window and it worked fine.

Is there a proper way to get rid of a previously declared variable, I thought that by clearing the console with clear() would do the trick?

Hi @chunzg,

Not that I know of, the clear console would not work here as it clears visible history only.
However if your goal is just to play around with it, you can copy your code across every time with different name, like: person1, person2 , person3 etc.

1 Like

Thanks that makes sense.