ES5 Custom Object Methods, Getters, and Setters

preview_player
Показать описание
There is a new shorter syntax for adding methods and properties to Objects in ES6. This video covers that new syntax as well as how you can add getter and setter methods for your object properties.

Комментарии
Автор

This video is still helping someone 3 years in the future understand JS a bit better. Thank you, Steve!

ihvtework
Автор

Thank you very much. Day by day, with every other your tutorial I found JS is not that formidable as before.

rotrose
Автор

What an awesome tutotial! Keep them coming!

Pharizer
Автор

Thank you very very much, it was like a flashlight in my brain. Finally, I could understand Getter and Setters!!!!

marcoscabrinirianidosreis
Автор

Awesome job! I was having trouble understanding Getters and Setters, but it makes sense now.

bretthoppough
Автор

Easy to understand and straight to the point, appreciate your explanation very much
!

kenho
Автор

Finally getter and setter make sense now. You rocks!

yehfang
Автор

You are a great teacher. Thank you for making these clear and helpful videos.

joebuono
Автор

Thank you Steve! I am happy that I found your channel. You are helping me a lot! Cheers!

maj
Автор

This is a great explanation, yet I really don't see much use of it. We could also access object property directly like obj.prop1 This encapsulation feeling is false because object properties are not really hidden like in real OOP environment. We can access them anyway. But as I said great explanation, thanks

adriatic
Автор

Thank you so much. It finally makes sense. Keep up the good work👌

yonisomar
Автор

thanks for the video! Greatly explained !

igorking
Автор

Thank you for the video. I have a quick question - Why use a getter? Could you not just write a 'normal method' that returned the value in prop1 * 2?

let obj = {
prop1: 1997,
prop2() {
return this.prop1 * 2;
}
};

console.log(obj.prop2());

kithowlett
Автор

Can you please tells us where we can use these getters and setters in real-world applications!

fahadhafeez
Автор

I haven't tested this yet, but normally when creating an object literal, all properties are public by default. If I remember correctly, there is no way of creating private fields in an object literal unless you change to a function constructor and use const/let for internal properties without using the this keyword or just use object descriptors. Does creating a getter and setter automatically creates the fields private? Also, what is the difference of creating a getter and setter vs plain methods that can return and set an object properties?

PS-dpyg
Автор

4:57 why don't we have to add '()' while calling the prop4 method in the obj unlike other methods in an object where we should add () to get the returned result

and why do I get an error when I call it with '()' like this obj.prop4()

romeojoseph
Автор

sir we cannot use set method separately like
let obj={
a:1979,
set c(_v){
this.a=_v;
},
prop3(){
console.log('prop3 called')
}
};

noaakshay
Автор

Hi Steve,
Thanks for the video! There are actually 2 things I don't get.
1. When I work in Java I usually just create a method called getValue() or setValue(). Why do we need the special keywords here?
2. Also, if the _prop1 is hidden from others, what's the point of still being able to set it?
Thanks for your answer in advance!

korhad
Автор

what if we write like obj.prop1(1980) in our setter function

romeojoseph
Автор

isn't it true that you can always view the javascript code (unless you're running it on a server via node)? If that's the case, you can alter the value of a variable without using the getters and setters right? So how are they then protected?

gekoskipatric