JavaScript Classes #2: Getters & Setters - JavaScript OOP Tutorial

preview_player
Показать описание
In JavaScript, you can use "getters" and "setters" within your class definitions to bind a function to a property. Both "getters" and "setters" behave like regular properties so you are able to get and set instance properties but you are able to define your own function to modify the behaviour.

They can be useful when you have a class with multiple instance properties and you'd like to compute/set a value while still maintaining loose coupling.

In this video I show you a few simple examples of how you can implement your own "getters" and "setters" on your user-defined classes.

For your reference, check this out:

Support me on Patreon:

Follow me on Twitter @dcode!

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

Your one sentence explained it well. "Getters and setters are used to define methods on a class, which are then used as if they're properties." Thank you so much!

jacobkupperman
Автор

Thanks for this! I was just wrapping up the freecodecamps 3 and a 1/2 hour long JavaScript tutorial and the lesson for this is not nearly as simple, or in-depth as this one.

robynjoseph
Автор

For me. This is the best explanation. And I used multiple sources. Thanks so much for doing this!

mcloughman
Автор

Brilliant explanation, setters/getters finally makes sense. Thanks!

nametakenfan
Автор

Watched this while reading the intro to objects module of mdn web docs, this vid gave me an easier way to think about getters and setters tbh (thanks a lot!!)

leeqv
Автор

Casually going through to say thanks for the series, great lessons

LetsGeTDeep
Автор

this tutorial helped me to better understand the concept of getters and setters. this helped me to solve my task and saved my time. thank you!

heavydirtysoul
Автор

This really helped explain the free code camp example.

erikT
Автор

my name is "DOM", very funny

waterybenis
Автор

thank you. it's so much clear now!

circomono
Автор

Great video & Silly question: Would it simplify things to add a field (_area) move the _area calculation to the constructor. In the getter returns: this._area. In the setter sets: this._area = area; ?
This would eliminate the need to find the square root needed to feed to the getter.

kenmtb
Автор

How is it possible to have 2 functions with the same name? How does the object know if you're calling the getter or the setter since you're calling both the same way? This is confusing.

sib
Автор

so, what's about class static getter & setter?

xgqfrms
Автор

If the area property didn't exist before writing your get area() method, then wouldn't that be a setter, not a getter?

Likewise, what you have as the set area() method currently sets the width and height properties according to a preexisting area. Wouldn't that be a setter for the width and and height? Not the area?

josephgreene
Автор

What's the relation of get and set to each other.

christianpaul
Автор

Hi, i'm using your getters and setters method to create my discord bot, but it keeps saying that my identifier is unexpected. Any idea, i'm far to be a good programmer.

Rhasgarrok
Автор

i like that keyboard what keyboard is that?

christianpaul
Автор

Hi nice video but please don't type at the very bottom of your window and increase your font so that people on a cell phone can read what your typing. Thanks!

someguy
Автор

I wish it was explained more theoretically then explained like this. Just because I'm studying this from codecademy, and I need better knowledge, but I haven't studied "class" and "new" yet. It's really hard to find the right tutorial for that

lunacollacchi
Автор

Bro you have 2 prototype functions called the exact same thing wtf?
1 get area and 1 set area?
they both have the same name, area


how can it work, if they have the same name should you not be accessing just one of them?
Bro help please

tntdogs