JS "this" and Function References - What is it all about?

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2018
  • The "this" keyword is the source of a lot of confusion in JavaScript world. So are function references (function calls without parentheses). What are "this" and those function references all about and why do you use them in JavaScript?
    ----------
    Read the article: academind.com/learn/javascrip...
    Starting Code: github.com/academind/js-this-...
    Finished Code: github.com/academind/js-this-...
    Learn JavaScript from scratch: acad.link/js
    Or take the "Accelerated ES6 course": acad.link/es6
    Want to learn something totally different? Check out all other courses: academind.com/learn/our-courses
    ----------
    • You can follow Max on Twitter (@maxedapps).
    • And you should of course also follow @academind_real.
    • You can also find us on Facebook.( / academindchannel )
    • Or visit our Website (www.academind.com) and subscribe to our newsletter!
    See you in the videos!
    ----------
    Academind is your source for online education in the areas of web development, frontend web development, backend web development, programming, coding and data science! No matter if you are looking for a tutorial, a course, a crash course, an introduction, an online tutorial or any related video, we try our best to offer you the content you are looking for. Our topics include Angular, React, Vue, Html, CSS, JavaScript, TypeScript, Redux, Nuxt.js, RxJs, Bootstrap, Laravel, Node.js, Progressive Web Apps (PWA), Ionic, React Native, Regular Expressions (RegEx), Stencil, Power BI, Amazon Web Services (AWS), Firebase or other topics, make sure to have a look at this channel or at academind.com to find the learning resource of your choice!
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @ernestodiaz4378
    @ernestodiaz4378 5 ปีที่แล้ว +13

    Uff, few people I've known, who can explain things so clearly and easily. Thanks for the video, it's awesome!

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

      Awesome to read that Ernesto, thanks a lot!

  • @CarlosUeda
    @CarlosUeda 2 ปีที่แล้ว +1

    Normally I don't make comments in videos. But I totally recommend Max's courses. It's the best way of improving your development skills.

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

    You're the one of my great teachers. Still I need your videos to watch everyday or memorizing what I've learned from you before.

  • @JMEPatterson
    @JMEPatterson 5 ปีที่แล้ว +6

    "Mind yoga" - I love it and it means you really are a Guru. Thanks for these videos. I got here backwards - I'm taking some of your Udemy courses and was so impressed that I came to TH-cam to see more.

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

      Thanks so much for both your fantastic feedback and your support here and on Udemy James! This honestly means a lot to me!

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

    Every time when I watch your videos I make sure, that your explanation style is the best for me and I am sure not only for me.
    Thank you and much success in your work.

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

      It honestly makes me happy to read such awesome comments Vahe! I try my best to explain as understandable as possible and it's just fantastic to see that this explanation style works for you and others :)

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

    I really learned a lot of things by watching this video. the things I did before without knowing its reason.
    Thanks a lot, Max

  • @Deliverant
    @Deliverant 5 ปีที่แล้ว +9

    the 'this' in javascript it's been the hardest thing to grasp correctly for me, thankfully we now have arrow functions. But it's also super important to know how the 'this' behave because there is a tons of legacy code out there

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

    Thanks, Max, Manu. Your video was a really nice refresher on 'this' keyword. Very clearly explained, as always in your videos.

  • @yuriishportko5935
    @yuriishportko5935 5 ปีที่แล้ว +30

    Max, THANK You very much, only now I understood what for bind() is

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

      Happy to read that the video made things clearer for you Yurii, thank you very much for your comment!

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

      At what point in the video does he cover the empty parthesis?

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

      *parenthesis

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

    Hi Max, I like the way you think and explain the content. i usually have a lot questions when follow a course but very little when I following your course as it is well explained. Keep up the good work.

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

    Adding this to my favorite videos. Thank you very much Max! 'this' starts to make more sense.

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

    Definitely the best explanation of 'this' I have come across. Thank you.

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

    I was confused this topic before but now it totally makes sense. Great video!

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

    Awesome Video! Really helped a lot to clear the confusion around this. Good work Max!

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

    This is the most clearest (yes i know that's not grammatically correct) video that explains both "this" and arrow function. Thank you for making this video, I finally get this solved. Thank you so so so so so much.

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

    Awesome explanation! There are many tutorials trying to explain this concept but found this tutorial very helpful and easy. Thanks!

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

      That's just fantastic to read, thanks so much Vugar!

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

    Wow!! What an amazing lesson!! I would eventually spend some 5h googling this thing around and still don't understand it well, if I hadn't watched this video. Thank you!!

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

      Happy to read that the video was helpful for you Gustavo, thanks a lot for your comment!

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

    This is this !! Very happy you have made this video Max nobody explain programming as you!!

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

      Thanks so much Pablo, just great to get such an awesome feedback!

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

    A lot of mind yoga here. Great one, Max

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

    No one could have explained this any better. you are the best!!

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

    when my company told to me to do certification from this guy on udemy I used to hate him. but now totaly depend on him to grab any type of knowledge.I have bought 4 courses of him.

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

    I simply LOVE the way you teach and the way you explain things. Huge thank you!!!

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

      Feedback like yours means so much to me, thanks a lot :)

  • @tanmaykrdey9293
    @tanmaykrdey9293 2 ปีที่แล้ว +1

    Great explanation. I really love Your courses. All the step by step explanation really helped me. Thank you.

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

    I just want to appreciate this content which is extremely helpful for every Javascript Developer.

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

    Came from the React - The Complete Guide. Awesome instructor.

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

    Awesome, now i understand the basics of this thanks to you. Thank so much Academind

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

    well done Max,this is the reason why in react component class you should bind method to allow it can be executed by the jsx

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

    Finally! I am finally beginning to "wrap my brain" around "this". Thank you Max. You are awesome

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

      YOU are awesome David, thanks a lot for sharing this great feedback!

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

    Max - I saved this video to tutorials I'm sure I will get back to in the future. Thanks !

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

    I counted 138 x 'this' in this. :)
    Great explanation, thanks Maximilian!

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

    Finally, I got "this"! Thanks you very much!

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

    I am now able to see why => functions help to resolve the problem with 'this'. I was reading it in theory and now it is with practical example. I really appreciate for the help Max.

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

      Just great to read that the video made things clearer for you, thank you very much for your comment!

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

    best explanation of this keyword found on internet.

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

    Very nicely explained the "THIS" keyword......:)

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

    The best explanation of this keyword. thank's a lot, max

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

    I'm sooooooooooo amaze how you explain things man, you are fricking amazing.

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

    A good refresher and great explanation! Thank you, Max!

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

      Great to read that you like it, thank you for your comment!

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

    You're my hero Max. At last i understood how the binding works! Working with React i always had this doubt of why i could use arrow functions without binding them in the constructor ☺

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

      Thanks so much Mauricio, so happy to read that :)

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

    That's what happens when you try to do multi paradigms(OOP/FUNCTIONAL) with a single language. A whole package of exceptions, yet beautiful. Nice video.

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

    Max you are bloody awesome! Thanks for explaining this SO clearly and in-depth

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

    great explanation Max.
    I fought a couple of times against “this” word but finally everything is much cleaner

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

      Awesome to read that the video helped to make things a bit clearer, "this" can indeed be confusing :)

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

    Great explanation! I remember there was another workaround before arrow functions appeared in ES6. You could say var that = this; within the constructor and then call that.addName() within button`s eventListener callback. I know, I know in 2022 probably no one use that dirt trick anymore :)

  • @user-gu5ts5nx8r
    @user-gu5ts5nx8r 3 ปีที่แล้ว

    Very helpful but challenging.
    Thank U.

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

    Thank you for this and all of your other videos! Incredibly helpful!

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

      This is really great to read, thank you very much!

  • @bwanamaina
    @bwanamaina 5 ปีที่แล้ว +7

    Thank you Max for clarifying this

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

      Happy to read that the video was helpful for you, thank you very much for your comment!

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

    Awesome explanation like always, Max!

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

    Wow..really really great explanation about this keyword

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

    Thanks for the video, very informative and closed some gaps :) I did have some problems with it before I even touched ES6 and didn't really know what's going on.
    One question. How would we go about passing something into the function we're only referencing, since adding parentheses would execute it immediately when the class instance is created? One thing that comes to mind is to call that function inside an anonymous function like what you actually did in your last example, is there any other way of doing it with less code?

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

    One of the best explanations out there. Thank you

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

    THANK YOU VERY MUCH MAX!! I was hung up on this specific concept.

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

      Happy to read that the topic is interesting for you Joe :)

  • @amrextracoding
    @amrextracoding 17 วันที่ผ่านมา

    Awesome explanation, Max!
    Coming from React js Course on Udemy

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

    as Kudvenkat is the best instructor on BE, you are the best on FE I guess. Keep going!

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

    Best explanation of this. Thank you!

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

    Awesome Video!!! Really appreciate your efforts. Because of you, I have got confidence in coding and have started my own channel . Thanks a lot Max!! Looking forward to learn a lot from you.. :)

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

    Amazing video!
    Best explanation I've found so far on this subject.
    Everything makes sense now.

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

      Wow, what an awesome feedback, thanks so much for that!

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

    Very good and clear explanation, thanks!

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

      Thanks a lot for your awesome feedback!

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

    Thank you - great explanation. Long live arrow functions!!

  • @wobsoriano
    @wobsoriano 5 ปีที่แล้ว +77

    this.video = 'awesome' ;

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

      Thank you Robert :)

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

      Or maybe () => { this.video = 'awasome'} to be on a safe side :(

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

      TypeError: Cannot read property 'undefined' of undefined at yt-formatted-string.video

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

      @@ahmedanalattack Yeah. That's what i do.

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

    This is gold. Thanks you, Max!

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

    this is GOLD! thank you so much for this great explanation, and for free! really unbelievable!

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

    Best explanation ever. Thanks, dude!

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

    Amazing explanation!

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

    Excellent explanation.

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

    Can anyone explain what is happening under the hood that causes the arrow functions to 'fix' the behavior of `this`?
    I understand what it does, but I don't understand why.

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

    Great explanation! Thanks!

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

    Great explanation Max you're a savior thank you

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

    Great description of both 'this' and arrow (or I've heard it also called a 'rocket' => in Ruby ;) ). I certainly would have been confused to see that notation.
    Is there any way to make that arrow function as a one liner?

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

    Can anybody explain ?
    When we use "this" keyword in react ? it should refer to class Name or Component?
    I am so confused in this keyword

  • @Albert-fe8jx
    @Albert-fe8jx 5 ปีที่แล้ว

    Great content. function versus function() -- a gold nugget. I'll have to rewatch 16:00 forward.

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

      Awesome to read that you like the video Alfred, thank you very much for your comment!

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

    very informative. my question is, do arrow functions not contain their own `this`, like normal function do ?

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

    great video and I appreciate the various solutions

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

    Super explanation!!

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

    Thanks Max! Quality content as usual!!

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

      So great to read that, thank you for your support!

  • @user-py4jl1nm3v
    @user-py4jl1nm3v 2 ปีที่แล้ว

    great lesson! thanks!

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

    Thanks max finally I understand this

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

    Great tutorial, thanks !

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

    Hi Max I am ur big fan. I have purchased ur react native course . I Suggest u please update ur course . It would be beneficial for every one.

  • @user-fj7bo8gh8e
    @user-fj7bo8gh8e 5 ปีที่แล้ว +1

    Perfect explanation Max! Like always... Just wanted to add something to the tutorial : my favorite way to deal with this-binding. If we include babel then we can declare the addName method in the NameGenerator class like this : " addName = () => {.....}". I like this approach because it looks so clean to me.

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

    What an excellent explanation! Thank you!

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

      Just awesome to read that you like the video Jessie, thanks a lot for your comment!

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

    Yes, another way would be do var self = this ;and pass self to the anonymous function. You can use self wherever you used this earlier in that scenario

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

    my brain is breaking but this helps out sooo much. Thank you kind sir 'this'

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

    Could also use class properties or apply a decorator that auto-resolves this.

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

    This is freaky. I was specifically having the exact problem with arrow functions vs. normal functions and how 'this' behaves differently today. And then this video appears when I get home.

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

      Guess that was kind of a good timing then, happy to read that Patrick :)

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

    Can we have a "this" counter on this video please.
    This video cleared the "this" concept. Thanks

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

    finally I know binding 'this', Thank you.Max.

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

      Happy to read that Brenda, thank you!

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

    Thanks sir !!!
    I got it
    Great explaination

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

    Muy buen video, todos los conceptos están bien explicados.

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

    I came to this video from Max's Angular course. Is this the same as for Angular(TS)?

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

    with the code as it is @16:51 , lets say addName() took parameters how would the addEventListener call look like ?

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

      good question... i think in that case, either you write an anonymous function that takes a param, or use that arrow syntax maybe ?

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

    this.currentName = ++this.currentName % this.names.length; :)) Nicely explained Max!

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

    Thank you Max !

  • @vk-fu7vg
    @vk-fu7vg 5 ปีที่แล้ว +1

    Awesome awesome stuff Max!

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

      Thank you :)

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

    Top notch video, Max

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

      Thank you David :)

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

    9:25 How does that code move like that?

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

    such a good teacher

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

    Thanks Max, fantastic video! also, i have a question, if i convert the method add name to arrow function like this addName = () => and remove the anonymous function from the event listener and remain it like it was before ('click', this.addName ), i got an error: "Unexpected token =" can you spot the reason for that?

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

      You need Babel to use class fields - they're not supported natively

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

    Amazing, informative content like always Max :)

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

      Thanks a lot William, happy to read that you like the video!

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

      Academind thanks for responding! You are my JavaScript hero lol. Are you available on any chat applications? I have a question I’d love to ask you. I been watching your Angular Material + firebase course on udemy. I am making an inventory management system and I am having a really hard time structuring my data. Could you give me some tips or maybe speak with me about it? Or should I ask my question on udemy?

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

    Max, your rock! If I don't become web developer with you, I don't know how else.
    I used the arrow function on line 20, with the addName function, it is another way that works just fine.
    class NameField {
    constructor(name) {
    const field = document.createElement('li');
    field.textContent = name;
    const nameListHook = document.querySelector('#names');
    nameListHook.appendChild(field);
    }
    }
    class NameGenerator {
    constructor() {
    const btn = document.querySelector('button');
    this.names = ["Max", "Anna", "Berny"];
    this.currentName = 0;
    btn.addEventListener('click', this.addName);
    }
    addName = () => {
    const name = new NameField(this.names[this.currentName]);
    this.currentName ++;
    if (this.currentName >= this.names.length) {
    this.currentName = 0;
    }
    }
    }
    const gen = new NameGenerator();

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

    Great explination! But, can you pass arguments by reference?

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

    soo perfect, thank you 👍👍