JavaScript Getters and Setters
🔥Get the COMPLETE course (83% OFF – LIMITED TIME ONLY): http://bit.ly/2M1sp4B
Subscribe for more videos:
https://www.youtube.com/channel/UCWv7vMbMWH4-V0ZXdmDpPBA?sub_confirmation=1
Want to learn more from me? Check out my blog and courses:
http://programmingwithmosh.com
https://www.facebook.com/programmingwithmosh/
Tweets by moshhamedani
Original source
22 responses to “JavaScript Getters and Setters | Mosh”
@Mosh you said that normal methods are read only and we can't use them to set values of properties, but can't we do something like this,
let person = {
firstName: "Muhammad",
lastName: "Farhan",
fullName () {
return `${this.firstName} ${this.lastName}`
},
updateFullName (name) {
[this.firstName, this.lastName] = name.split(' ')
}
}
person.fullName() // Muhammad Farhan
person.updateFullName('Mosh Hamedani')
person.fullName() // Mosh Hamedani
I still don't get the use of getters and setters in JavaScript.
That whole setter splitting firstName and lastName with the index is a trash workflow
This is a great video but Javascript itself is a steaming pile of dogshit dont at me
ok… just so I'm clear… the only benefit of using a getter.. is so that you don't have to call it with parentheses?
a little bit hard to understand
is there any easy way???
i sort of get this explanation… but not 100% about this my mind is a bit torn
I don't understand why everyone can't explain this topic in the same way. Brilliant. Thank you very much!
What ide/code editor are you use?
Great video, thanks mosh
So much better then CodeCademy but without practice I'm gonna forget this after a day :((
And nowhere to practice :'(
i swear i did this code right but i keetp getting the 'invalid shorthand propety initializer' error. where can the problem be?
Why is it person.firstName and not this.firstName? I know they both work but… PS: Going to watch the next video suggested that talks about "this" 😀
hi mosh you have an amazing style in explanation
but your name aroused
my curiosity . are you from arabic origins ?
This is awesome. I get () {
this._now
} x
Great explanation! Thank you.
you could also use:
set fullName(value) {
[this.firstName, this.lastName] = value.split(' ')
}
Excellent. Ty alot
In the setter method you can use the new destructuring operator shorthand for arrays like this:
[firstName, lastName] = value.split(' ');
For more info: http://exploringjs.com/es6/ch_destructuring.html
Thanks Mosh
This video answered ALL my questions on getters & setters. Thanks so much!
You have no idea how long i tried to understand this
I like this explanation, because on teamtreehouse.com, I was confused and they didn't explain why and when we use getters and setters clearly