JavaScript Getters and Setters Explained

preview_player
Показать описание
In this tutorial we'll take a look at JavaScript Getters and Setters and see how you can use them with both the ES6 Class syntax and also with the more traditional way to define JavaScript classes.

JavaScript Getters and Setters Explained
-----------------------------------------------------------------
00:00 Introduction
00:46 Creating a new Class
01:41 Adding a setter
03:09 Adding a getter
03:27 Adding value to Getters and setters
06:53 Non-ES6 getters and setters

— Follow Me —
— Thanks! —

So in this JavaScript tutorial we'll take a look at how, with a basic JavaScript class definition, you can create custom behaviour for when you assign properties to an object and also retrieve their values.

We'll start of the tutorial with a simple bit of code that demonstrates how classes can be used to create new JavaScript objects and assign properties to the new object that is created.

We'll then define custom behaviour for our object's properties, adding validation for when we assign a new value to a property and also returning a different value for a property that has already been assigned a value.

Once we've taken a look at the ES6 getters and setters on a class, we'll see how this works with the older, more traditional way of assigning new properties to a class with a function constructor in JavaScript.

Check out some more Junior Developer Central JavaScript tutorials on the rest of the channel. Channel Handle @codebubb
Рекомендации по теме
Комментарии
Автор

I've watched so many videos on this topic, read articles, and nothing made me understand this more than you're explanation has! Subbed!

p-townhero
Автор

Finally a tutorial on the subject that makes sense! Thank you SO much!

sofiatachmatzidou
Автор

This is the best video on youtube currently on this topic. Thank you for this! Glad I found this channel.

aneeketkumar
Автор

If i would have seen this video 3 years ago I would have instantly understood getters and setters lol. I didnt understand this for sooo long. More teaching like this needs to be put out there for the self taught devs!

alexzajacek
Автор

This is such an underrated channel! Sometimes I don't understand the algorithm of YT, I see some loser on this platform that filming videos like "PHP in 2020!?", "JS in 2020!?, " in 10 min blabla for nothing then you learn simply nothing as well. I hope you will much more followers than this. I humbly bow before the extraordinary work you do each day. ! #serpect!

kenansari
Автор

Awesome explanation––you made it so simple to understand these concepts!

gagesalicki
Автор

1:24 Your mention of my name in your code is the first time in history ;) I myself use John :0

alimirazimzadeh
Автор

Ty for this video. Its awesome. Very good explain) Sorry for my Eng, i am from Belarus and i am watching your videos for learning JS. Dont stop, you are the best)

andreiostapenko
Автор

Thank you very much 👍 I didn’t understand getters and setters on codecsdemy

zyshawn
Автор

i am not getting an error if i do this
class myname{
set setname(value)
{
this.name=value;
}


}
var my=new myname();
my.name="hello";
console.log(my.name)
hello is printed and i had not found any
error in it

pawangaur
Автор

Awesome video man. I have a doubt though: In the es5 part why are we setting "name" on User.prototype and not on User ?

iceman
Автор

what's the difference between using the keyword set and just straight up writing a get method like a regular function?

nemodot
Автор

Eu moro no Brasil, entendo muito pouco de inglês e esse vídeo me ajudou muito.

ananascimento
Автор

Very good. Thanks a lot 👏 - in the end you could also go the other way so instead of traditional example show the typescript version that allows to protect `._password`

norfeldt
Автор

Nice, however creating getters and setter with arrow functions while explaining how to build them without ES6?

hellor
Автор

Very cool explanation! Thank you! /* by the way const is not allowed if it's not es6 isnt't it? */

magistrfox
Автор

Can you make a video in which if a condition is false then by giving && other will happen I mean
👇
( ! Condition && 'display this' )

_.sunnyraj._
Автор

I was confused why Getters and Setters weren't working in a Constructor function of an object and this tutorial cleared the confusion succinctly. Just one suggestion, it's better if you speak a little slowly. That'd help non-native English speakers to follow your videos better.

priyamganguly