JavaScript Getters and Setters | Mosh

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 พ.ค. 2018
  • JavaScript Getters and Setters
    🔥Get the COMPLETE course (83% OFF - LIMITED TIME ONLY): bit.ly/2M1sp4B
    Subscribe for more videos:
    / @programmingwithmosh
    Want to learn more from me? Check out my blog and courses:
    programmingwithmosh.com
    / programmingwithmosh
    / moshhamedani

ความคิดเห็น • 268

  • @k.safonov
    @k.safonov 5 ปีที่แล้ว +78

    I don't understand why everyone can't explain this topic in the same way. Brilliant. Thank you very much!

    • @erhanertem196
      @erhanertem196 2 ปีที่แล้ว

      His voice is commercial. Everybody buys it first. When voice is welcoming, with the right attitude, pace, bang-on content, psychological impact of close shot of what he is doing (not the entire screen area) and proper-concise-basic explanation would have and certainly had brought him above all the alternate explanations. That's my friend called an educator tapping the maximum.

    • @Retrofire-47
      @Retrofire-47 ปีที่แล้ว +1

      @@erhanertem196 jeeze, someone has a crush

  • @JeremyGalloway
    @JeremyGalloway 5 ปีที่แล้ว +420

    So much better than the CodeCademy explanation!

    • @Code325
      @Code325 5 ปีที่แล้ว +66

      I just came here from CodeCademy too, and I totally agree!

    • @ravabat5841
      @ravabat5841 4 ปีที่แล้ว +5

      It's true

    • @samrad6508
      @samrad6508 4 ปีที่แล้ว +27

      it's also better than the FreeCodeCamp explanation !

    • @iamlasbrey6334
      @iamlasbrey6334 4 ปีที่แล้ว +19

      lol, also coming from freecodeacademy

    • @Djolesabijacica
      @Djolesabijacica 4 ปีที่แล้ว +1

      yeah

  • @TheJackTheLion
    @TheJackTheLion 2 ปีที่แล้ว +9

    You know why this is great? Because instead of just telling someone what to write and blow their mind you actually explain HOW what you are explaining came to be and WHY you would need it. Not just write this and try to get this. That is the x factor when it comes to coding tutorials. Well done as usual Mosh, I hope that made sense.

  • @annasezen5357
    @annasezen5357 5 ปีที่แล้ว +105

    you could also use:
    set fullName(value) {
    [this.firstName, this.lastName] = value.split(' ')
    }

    • @1polyron1
      @1polyron1 4 ปีที่แล้ว +9

      Good shit.

    • @sunspear1813
      @sunspear1813 4 ปีที่แล้ว +3

      Although it's a good practice and neat trick, I think it's kind of showoff to write comments like this.

    • @noahpeltier
      @noahpeltier 4 ปีที่แล้ว +35

      @@sunspear1813 How else was Anna supposed to share this helpful information? You can't say that commenting alternate methods that seem better is showing off, then teachers would always be showing off, everyone with superior knowledge would be "showing off". In fact, Mosh is showing off by posting these videos! HOW DARE HE!

    • @darksnow1111
      @darksnow1111 4 ปีที่แล้ว +4

      @@noahpeltier YEAH then in this case the whole ES6 is show off, why should we use arrow functions!!!

    • @3riodokaos333
      @3riodokaos333 4 ปีที่แล้ว +5

      SunSpear said that because Anna is a woman. If it was a man's comment, he wouldn't say that. Thanks Anna and sorry for that. Keep commenting and "showing off". You help a lot of people that way.

  • @RyanGordy1
    @RyanGordy1 2 ปีที่แล้ว

    Mosh should teach every course, no fluff, straight to the point!

  • @julibeeee408
    @julibeeee408 5 ปีที่แล้ว +3

    This video answered ALL my questions on getters & setters. Thanks so much!

  • @ahmadraja5426
    @ahmadraja5426 9 หลายเดือนก่อน

    Thank you for explaining this from first principles.
    Starting with a simple object, giving scenarios for why you'd need to implement this, and going from there.

  • @alexlun4464
    @alexlun4464 4 ปีที่แล้ว +3

    You're wonderful at explaining topics like these, bravo!

  • @Baltie3
    @Baltie3 ปีที่แล้ว

    Very simply explained! Formerly I got lost when other people described the same topic. Thank you very much!

  • @zakhariihusar6975
    @zakhariihusar6975 3 ปีที่แล้ว +1

    Glad you started this video with a reason why we need this👍

  • @dianaordonez6747
    @dianaordonez6747 3 ปีที่แล้ว

    Amazing! I understood this better than others spanish channels. Thank you

  • @Calupp
    @Calupp 2 ปีที่แล้ว +5

    thank you, my current courses' explanation of how this works and its' use case was super clumsy.

  • @armandvermeulen8221
    @armandvermeulen8221 ปีที่แล้ว

    This made so much more sense, thank you for the video. Hope you are doing well.

  • @KleberJS
    @KleberJS 5 ปีที่แล้ว

    I like so much your explanation about javascript. I always can understand properly

  • @natefr0st239
    @natefr0st239 4 ปีที่แล้ว +2

    Simple and clean explanation, lovely!

  • @echelonanglo2226
    @echelonanglo2226 2 ปีที่แล้ว

    Only explanation i could find out there that made sense to me! amazing, thank you

  • @iaml2909
    @iaml2909 4 ปีที่แล้ว +1

    This help me a lot.
    Thank u very much. I will share this with my telegram group. Thanks again, very helpful.

  • @modestas2890
    @modestas2890 6 ปีที่แล้ว +3

    very clear explanation. Thank you.

  • @krisilyubenov2372
    @krisilyubenov2372 9 หลายเดือนก่อน

    I can't believe I was able to understand this in 6 minutes compared to 3 hours of lectures. Saved my life for the exam, thanks lol

  • @alisasenicic919
    @alisasenicic919 3 ปีที่แล้ว

    Great explanation!!! All videos are really great explained! Great teacher skills!

  • @raphaelugha5348
    @raphaelugha5348 4 ปีที่แล้ว +1

    Simple & clean. Loving it! Keep it up!

  • @nour.m8205
    @nour.m8205 ปีที่แล้ว

    Awesome explanation! I was struggling a bit with this concept. Thanks!

  • @dianalisova2669
    @dianalisova2669 3 ปีที่แล้ว +1

    Thank you so much for this clear explanation!

  • @guoxinn
    @guoxinn 5 ปีที่แล้ว +17

    Thank you for the awesome explanation, John Smith!!

    • @dgua2917
      @dgua2917 4 ปีที่แล้ว

      😂😂😂

  • @climbers1376
    @climbers1376 5 ปีที่แล้ว +14

    Getters and Setters:
    2:38-3:04, contextual use case
    3:05-3:14, purpose/rule
    3:14-5:37, how to prepare

  • @BHVampireLF
    @BHVampireLF ปีที่แล้ว

    Ohhhh, so that's the difference, I finally understood, thank you very much. 💕 Awesome explanation.
    This is the kind of stuff I read before for the question "What's the difference/advantage of a getter over a simple method?", I don't know if it resolves the question because I didn't understood a thing:
    -"Given that without a getter, all we can do is poll the attribute for its value, anything else would require a special method which we would have to invoke on the value. Not insurmountable, but will take some thought."
    Reading that again made me forget what I saw in the video, I need to rewatch. 😂

  • @Chris-ou4ej
    @Chris-ou4ej 4 ปีที่แล้ว +2

    Thank you for clear explanation.

  • @ZakiZaki-eo7qh
    @ZakiZaki-eo7qh 2 ปีที่แล้ว

    very informative and straight to the point thank you so much !

  • @ronpalmer7260
    @ronpalmer7260 7 หลายเดือนก่อน

    Love your Christmas ad. Its epic.

  • @anaraberda4677
    @anaraberda4677 2 ปีที่แล้ว

    thank you very much for this video. I couldn't understand this topic as I am learning at codeacademy. You managed to explain it in a simple logic way. So happy to come across you video

  • @Pareshbpatel
    @Pareshbpatel 2 ปีที่แล้ว

    An excellent illustration of Getters/Setters in JavaScript. Thanks, Mosh
    {2021-09-22}

  • @Alex.Shalda
    @Alex.Shalda 9 หลายเดือนก่อน

    perfectly explained, thank you!

  • @chapman8369
    @chapman8369 5 ปีที่แล้ว

    thanks for explaining, finally understand setters

  • @shubhamsarohi9231
    @shubhamsarohi9231 4 ปีที่แล้ว +1

    Hi Mosh,
    The getter setter implementation in this video is different from the one in your course of OOP.
    Can you please explain which one is better?

  • @seekingcode
    @seekingcode 5 ปีที่แล้ว

    Great explanation! Thank you.

  • @kimken8296
    @kimken8296 3 ปีที่แล้ว +1

    amazing explanation!! Thanks so much!

  • @shauncomino5849
    @shauncomino5849 6 ปีที่แล้ว

    so helpful. very glad i came here

  • @YoBarbados
    @YoBarbados ปีที่แล้ว

    Fantastic brother thank you very much!

  • @xesiusprime9360
    @xesiusprime9360 3 ปีที่แล้ว

    your explanations are top notch thanks dude

  • @mayurapathak1654
    @mayurapathak1654 3 ปีที่แล้ว

    Thank you ! very clear explanation.

  • @_schemaki_
    @_schemaki_ 10 หลายเดือนก่อน

    with in 5 minutes Mosh helped me to understand the topic 😀

  • @ToddsDiscGolf
    @ToddsDiscGolf 2 ปีที่แล้ว

    This video really helped solidify the ideas after I spent hours on the codecademy “meal maker” project! Appetizers, mains, and desserts, oh my!

    • @jonjoio9184
      @jonjoio9184 2 ปีที่แล้ว

      Oh thank god not just me. I eventually gave up and followed the video and my code still doesn't work. I've been loving the course so far but this was so weird.

  • @mateuszjamroz9994
    @mateuszjamroz9994 4 ปีที่แล้ว +3

    Hi nice tutorial, but why can't we do the smae task whitout naming those functions get and set?

  • @mohamedtawfik7344
    @mohamedtawfik7344 ปีที่แล้ว

    Great , professional way

  • @Camel_Coder
    @Camel_Coder 2 ปีที่แล้ว

    Great explanation, thanks!

  • @Karlponken
    @Karlponken 2 ปีที่แล้ว

    Quick and good explanation, thanks alot!

  • @ripplesr5655
    @ripplesr5655 3 ปีที่แล้ว +1

    on spot! Thanks a lot for this Mosh!

  • @Tuttigiu
    @Tuttigiu 3 ปีที่แล้ว

    For learning purposes I used Get to return a value of an outside variable but then I couldn't Set it.
    Is there a way to do so ?
    if I simply use :
    get abcde( ) { return outsidevariable }
    set abcde(valuenew) { outsidevariable = valuenew }
    and then outside I type:
    object.abcde = `whatever`
    keeps returning the outsidevariable original value and not the `whatever` I tried to set.

  • @Wangaruro
    @Wangaruro ปีที่แล้ว

    Wow! That's well explained 👏🔥

  • @jameshayes3810
    @jameshayes3810 2 ปีที่แล้ว

    Great explanation. Thank you so much. I have ticked the bell icon :)

  • @shakeelahmedsiddiqi
    @shakeelahmedsiddiqi 6 ปีที่แล้ว +1

    Awesome video for beginners.

  • @hodayamirr
    @hodayamirr 2 ปีที่แล้ว

    tried to find the next lecture's video and couldn't, can i get a link or the name of it? thanks!

  • @ibrahimsinan7994
    @ibrahimsinan7994 4 ปีที่แล้ว +1

    Thank you Mosh! AMAZING explanation. Do you have courses on Udemy?

  • @royrobinson5197
    @royrobinson5197 6 ปีที่แล้ว

    Hello Mosh thanks for this great tutorial on functions, please can you also make a video on how to use anonymous functions in JavaScript?

  • @Ma1ne2
    @Ma1ne2 3 ปีที่แล้ว +2

    How is this related to classes? I know this concept from back in the days when I learned Java to access class attributes. Here I can also do it with objects and that's great, but how is this related to the new ES6 classes in JS?

  • @kentarotanaka568
    @kentarotanaka568 5 ปีที่แล้ว

    Thank you!!! This is 1000x better than Treehouse's video on getters and setters. Their instructor they currently have is horrible. Plus it doesn't help when their sample code are using the same variable name that's confusing the heck out me. "get owner(){....} set owner(owner){this._owner = owner; ....}. What the heck is going on! Also thanks for going over pre es2015 code. I would like to know or at least recognized the previous format when I come across it.

  • @Mooblr
    @Mooblr 5 ปีที่แล้ว

    Great video, thanks mosh

  • @oghenefokethompson7407
    @oghenefokethompson7407 4 ปีที่แล้ว

    hi mosh you have an amazing style in explanation but please i need your data structure course in JavaScript

  • @boozflooz6255
    @boozflooz6255 ปีที่แล้ว

    That's awesome, understood it fast because of your explanation. What theme are you using, btw?

  • @evans8245
    @evans8245 6 ปีที่แล้ว

    bless u, please, never stop what u are doing, im picking this lifeskill and frankly, thanks alot

  • @HassanMalikTW
    @HassanMalikTW 6 ปีที่แล้ว +1

    amazing explanation

  • @bligotblam
    @bligotblam 3 ปีที่แล้ว

    What is this setup to get the output like that in VSC?

  • @p1k1m4ru
    @p1k1m4ru 2 ปีที่แล้ว

    i really like this explanation - u earn my sub badman!

  • @paudelamarianaoancea3761
    @paudelamarianaoancea3761 4 ปีที่แล้ว

    is this. refereing to the person object ? or to the fullName method ?

  • @sonamuhialdeen7866
    @sonamuhialdeen7866 3 ปีที่แล้ว

    Awesome tutorial

  • @coced
    @coced 5 ปีที่แล้ว +1

    You have no idea how long i tried to understand this

  • @homeofstasis
    @homeofstasis ปีที่แล้ว

    Insane explanation🌟

  • @eklavyasharma
    @eklavyasharma 4 ปีที่แล้ว

    How do you test code in the chrome side by side?

  • @Mikef05316338
    @Mikef05316338 2 ปีที่แล้ว

    Thank you so so much. Just...thanks man!!!

  • @joshel4278
    @joshel4278 ปีที่แล้ว

    Please I have a question....
    Why does the "this" keyword work when we put them into objects when initializing since objects cannot be accessed before initialisation?Is it because of hoisting,please just explain this to me...

  • @andreekzz
    @andreekzz 5 ปีที่แล้ว

    Excellent. Ty alot

  • @TheGabrielMoon
    @TheGabrielMoon 6 ปีที่แล้ว

    Can you send exercises to pratice this features?

  • @TarekFaham
    @TarekFaham 4 ปีที่แล้ว

    What version of javascript for this video?

  • @sakshigosain238
    @sakshigosain238 ปีที่แล้ว

    only half way in, but wow what an amazing explanation

  • @fredpourlesintimes
    @fredpourlesintimes 4 ปีที่แล้ว

    Good explanation.

  • @priyamganguly
    @priyamganguly 3 ปีที่แล้ว

    Can Getters and Setters be applied within Constructor functions of objects? I tried, but it's not accepting them in a Constructor. Please explain us if there's a way.

  • @rabeehebrahim
    @rabeehebrahim 2 ปีที่แล้ว

    Thanks Hamadani!

  • @russ_the_ceo6456
    @russ_the_ceo6456 ปีที่แล้ว

    Thank you!

  • @yuvrajshukla4616
    @yuvrajshukla4616 3 ปีที่แล้ว

    nice explanation

  • @geovaniraffaelli4508
    @geovaniraffaelli4508 5 ปีที่แล้ว +2

    Thanks man, couldn't understand the example from Eloquent JS chapter 6 but this is crystal clear. You're the best.

    • @hzmi_
      @hzmi_ 5 ปีที่แล้ว

      I couldn't understand anything from Eloquent JS lmao

    • @hzmi_
      @hzmi_ 5 ปีที่แล้ว

      Maybe. I think the examples were a little too complex for me in Eloquent JS. Anyway, your video was helpful! Thank you Mosh.

    • @geovaniraffaelli4508
      @geovaniraffaelli4508 5 ปีที่แล้ว +1

      Hey iMpetus. Even though it's a complex book and sometimes you have to reach for other resources in order to fully comprehend things, I still 100% recommend it because once you get through it you'll have a solid understanding of the concepts.

    • @hzmi_
      @hzmi_ 5 ปีที่แล้ว

      Thanks for sharing your insight Nanika. I will give it another try. :)

    • @147dileep
      @147dileep 5 ปีที่แล้ว

      @@programmingwithmosh sir, why you always use "const" rather than "var" or "let"...?

  • @contactmerrou
    @contactmerrou 3 ปีที่แล้ว

    Hey Mosh, i'm wondering, why aren't you using the this keyword in the return of the setter property?

  • @fefuzii
    @fefuzii 2 ปีที่แล้ว

    Thanks a lot man

  • @luis96xd
    @luis96xd 3 ปีที่แล้ว

    Whoa, nice video, it was a great explanation 😄

  • @vedchoudhary9519
    @vedchoudhary9519 6 ปีที่แล้ว

    Thank you sir

  • @raphaelguedj5671
    @raphaelguedj5671 3 ปีที่แล้ว

    I just came back from CodeCademy to better understand getter and setter.
    So getter is just to be able to call a method like a property ? Codecademy mentionned this is relate to privacy (with the convention _prop) . is it also linked ?

  • @KirbyLWallace
    @KirbyLWallace 3 ปีที่แล้ว

    I am assuming that nothing protects firstName and lastName from being updated directly? There is no way to protect the name properties?

  • @Gwgitu77
    @Gwgitu77 5 หลายเดือนก่อน

    Thanks a lot

  • @komybro4208
    @komybro4208 2 ปีที่แล้ว

    This guy is good at explaining

  • @noorulameen7688
    @noorulameen7688 ปีที่แล้ว

    In setter without using the variable name const the split return an string
    Why it happens?

  • @xgqfrms
    @xgqfrms 3 ปีที่แล้ว

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

  • @jyothikrishn1202
    @jyothikrishn1202 3 ปีที่แล้ว

    Can we specify getter and setter to a constructor function ?

  • @joeya2330
    @joeya2330 3 ปีที่แล้ว +12

    English subtitles: Mosh Hamedani ( Marsh comma daddy )

  • @sk0vik
    @sk0vik 2 ปีที่แล้ว

    Does anyone know how to do this part
    set fullName(value) {
    const parts = value.split(" ");
    this.firstName = parts[?];
    this.lastName = parts[?]
    }
    if someone's first and/or last name is more than 1 word => so not parts[0] and parts[1]? Like for example "Juan García Reyes" where "García Reyes" is the last name, or "Anne Marie Smith" where Anne Marie is the first name.

    • @pipitgusmayanti8342
      @pipitgusmayanti8342 2 ปีที่แล้ว

      this.firstName = parts.slice(0, parts.length - 1).join(" ");
      _// slice from index 0 until before the last index_
      this.lastName = parts.slice(1).join(" ");
      _// slice from index 1 until the end of array_
      _// join(" ") joins all elements into a string, each element separated by " "_

  • @oshadhaedirisinghe1455
    @oshadhaedirisinghe1455 27 วันที่ผ่านมา

    Thank you

  • @Dhhddsisi
    @Dhhddsisi 2 ปีที่แล้ว

    I don't understand the setting new full name part. Why did it work with just person.fullName = 'John Smith' ? Shouldn't it be person.fullName( 'John Smith' ) ?

  • @rubayaat54
    @rubayaat54 5 ปีที่แล้ว

    Thanks Mosh

  • @sidequestuk
    @sidequestuk ปีที่แล้ว

    Really god thank you!

  • @DUO_QUEST
    @DUO_QUEST 4 ปีที่แล้ว

    Now it's clear to me :)

  • @ahmadabdolsaheb
    @ahmadabdolsaheb 5 ปีที่แล้ว

    thanks for the video, do you also speak farsi ?

  • @zakariab7187
    @zakariab7187 หลายเดือนก่อน

    Finally i get it from this video