JavaScript this Keyword

Поділитися
Вставка
  • Опубліковано 14 тра 2018
  • JavaScript this Keyword
    🔥Get my complete JavaScript course: bit.ly/2M1sp4B
    Subscribe for more videos:
    / @programmingwithmosh
    Want to learn more from me? Check out my blog and courses:
    Courses: codewithmosh.com
    Blog: programmingwithmosh.com
    Facebook: / programmingwithmosh
    Twitter: / moshhamedani

КОМЕНТАРІ • 537

  • @MosheSchnitzler
    @MosheSchnitzler 3 роки тому +113

    "This" was the first word I said as a baby. I can't believe that I'm still stuck here...

    • @daniyahaider6468
      @daniyahaider6468 3 роки тому

      lol

    • @sohailaali2081
      @sohailaali2081 2 роки тому

      Did figure it out what exactly is it?? Plaeseeeeeee explain me in simple words😭😭

    • @CapeSkill
      @CapeSkill 2 роки тому +2

      @@sohailaali2081 The keyword ''this'' is basically an object that is binded to the function that is being executed.

    • @sohailaali2081
      @sohailaali2081 2 роки тому

      @@CapeSkill yeah thanks but I already had figured it out last month😂😂❤️

    • @CapeSkill
      @CapeSkill 2 роки тому +1

      @@sohailaali2081 It's not really as difficult as people make it seem to be.

  • @nithin1674
    @nithin1674 3 роки тому +73

    "this" ain't stopping me from going forward in my path.

  • @nyxmediaentertainment8723
    @nyxmediaentertainment8723 4 роки тому +190

    " 'this' references the object that is executing the current function" ..simple, yet enlightning.. thanx!

    • @Ethan-hm4qr
      @Ethan-hm4qr 3 роки тому +4

      Thanks! This saved me from a lot of confusion

    • @akashkrishna1873
      @akashkrishna1873 3 роки тому

      Me too commotion solved

    • @NINJA29A
      @NINJA29A 2 роки тому +1

      “this” is a very useful keyword, I didn’t know it until now I wrote 5 lines of code to do its job.
      instead I could use “this” one keyword, lol.

    • @mohitrajput6007
      @mohitrajput6007 2 роки тому +2

      This simple line explains the entire concept

    • @crystall_sh
      @crystall_sh Рік тому +1

      It's not that simple you have to know it's properties where to implement it..

  • @MsElsospechoso28
    @MsElsospechoso28 5 років тому +773

    THIS is where I quit programming.

    • @ecosunflower1892
      @ecosunflower1892 4 роки тому +21

      I can feel you. Not really noob friendly but you can do it!

    • @ganeshchokhare7525
      @ganeshchokhare7525 4 роки тому +5

      first watch this, It will help you ua-cam.com/video/n_9oUP1GQz0/v-deo.html

    • @justafreak15able
      @justafreak15able 4 роки тому +13

      How are you on every single video I watch?

    • @jagdeesh9338
      @jagdeesh9338 4 роки тому +3

      😂😂nice comment

    • @memeyan9874
      @memeyan9874 4 роки тому

      i think the "this" issue on javascrpt may just a historicol probrem from the source code,on me case,when i learn python code,i never have any kind of probrem like this

  • @oloyang431
    @oloyang431 Рік тому +14

    One thing you forgot to mention is that if you use an arrow function instead regular function when using a forEach() method in your example the keyword "this" actually refers to the object, and not the global window. So basically, "this" acts differently depending on whether you're using a regular function or an arrow function.

    • @marvinalone
      @marvinalone Рік тому

      because arrow function creates a closure, it's similar to
      var that = this
      this.tags.forEach(function(tag) {
      console.log(that.title, tag)
      })

    • @AkashhGuptaa
      @AkashhGuptaa Рік тому +1

      @@marvinalone man your keywords tags that this really confused me

    • @fleurdecerisier9550
      @fleurdecerisier9550 5 місяців тому

      😢😢

  • @kduisheev4355
    @kduisheev4355 3 роки тому +197

    still could not manage to explain to my 7 yrs old brother.

    • @sukhdevduhariya3280
      @sukhdevduhariya3280 3 роки тому

      hahaha

    • @mohammaddh8655
      @mohammaddh8655 3 роки тому +12

      i don't know about your brother but I'm 24 and I'm fucked

    • @ayoubZaZen
      @ayoubZaZen 3 роки тому +1

      @@mohammaddh8655 same hhhhhhhhhhhhhhhhhh

    • @bryanurizar
      @bryanurizar 3 роки тому

      What a dumbass. A 7 year old struggling to understand this.

    • @awekeningbro1207
      @awekeningbro1207 3 роки тому +12

      i tried to explain this to my 7 year old brother, then i realized i don't have one.

  • @iLoveTurtlesHaha
    @iLoveTurtlesHaha 5 років тому +13

    Honestly, this makes so much sense to me. When people were telling me the 'this' in JavaScript is complicated I was worried that I would struggle to learn it but it makes so much sense if you stick to the rules for methods, functions, and constructor functions.

  • @prabdeepdhaliwal9725
    @prabdeepdhaliwal9725 3 роки тому +18

    So from my understanding:
    NOTE:
    A *method* is a function within an object.
    A *callback function* is a function passed as an argument/parameter within another function.
    ==============================================================================
    1. The *'this'* keyword *refers to the global object* (window) when using 'this' *within* a *function*
    Ex. function Hello() {
    console.log(this)

    • @DS-rv2fc
      @DS-rv2fc Рік тому +1

      honestly, just use an arrow function instead of inserting "this"

    • @burekburczynski9802
      @burekburczynski9802 Рік тому

      @@DS-rv2fc hi, where is the next lecture of 'this' topic? i searched on YT list but I see nothing to be continue of 'this' lecture...

    • @user-xw3uj8ij6v
      @user-xw3uj8ij6v 2 місяці тому

      It's a myth that content creators keep copying. An object does not own a function, it just has a reference to a function no matter where we declare the function inside the object or outside. The "this" binding is due to 'the dot notation' which is described by ECMAScript standard "13.3.2 Property Accessors" "theObject.theProperty".
      function Hello() {
      console.log(this)
      }
      const obj = {
      hello : Hello
      }
      obj.hello() // this is obj
      const myHello = obj.hello;
      myHello() "this" will be undefined if it's 'use strict' or global object depends on host environment (node.js , browser, ect.)
      example with forEach just special case of no dot notation, arrow functions fix that because that kind of functions use parent environment which was called with dot notation)

  • @bileljribi6977
    @bileljribi6977 5 років тому +107

    I rarely make comments on UA-cam, I just want say THANK YOU, this is by far the best explanation for the "this" keyword I have found!

  • @ganeshbabu6458
    @ganeshbabu6458 3 роки тому +10

    Without you, I wouldn't have learnt Angular. You're the best lecturer out there Mosh...

  • @rodrigoribeiro377
    @rodrigoribeiro377 4 роки тому +4

    THIS is an incredible explanation... not only about the reserverd word 'this' but also about the behaviour of structures that uses 'this' such as constructor functions. It cleared my mind. Thanks!

  • @taleoftravels6701
    @taleoftravels6701 4 роки тому +105

    Trying to summarize 'this' after watching this video for at least 5th time:
    For fns inside Objects: 'this' refers to the object calling the function.
    For fns not called by objects - Eg call back functions - 'this' refers to the global object.
    For fns defined using arrow functions - 'this' simple refers to the object that is executing the arrow function - Irrespective of object's scope. Meaning: if the arrow function is called from a global scope? 'this' will refer to the global object. If the arrow function is called with in a function which belongs to an object?'this' will refer to the calling function's scope and in this case the object itself.

    • @dingdong8704
      @dingdong8704 4 роки тому +8

      you confused about what i had learnt till now, thanks to me for reading your comment.

    • @nirajankhadka2661
      @nirajankhadka2661 4 роки тому +4

      Can somebody summarize this comment further for me plss

    • @theSUBVERSIVE
      @theSUBVERSIVE 4 роки тому +7

      I think it means that in the last example, if instead of:
      1) this.tags.forEach(function (tag) {console.log(this.title, tag}, this)
      2) this.tags.forEach((tag)=>{console.log(this.title, tag)}
      he could get the same result by using an arrow function instead because it would inherit the _this_ from above, calling the Object itself

    • @thanachon8892
      @thanachon8892 3 роки тому +3

      "THIS" is all summary that I want for enlightened myself. Thank you!

    • @omageprosper9673
      @omageprosper9673 2 роки тому

      thanks

  • @swethanaik7121
    @swethanaik7121 3 роки тому +9

    "This" is so well explained. Such an eye opener since I have always been confused with the "this" keyword in Javascript since it can mean different things depending on its placement unlike in Java which is pretty straightforward.

  • @JeremyGalloway
    @JeremyGalloway 5 років тому +45

    0:25 hands down best "this" definiton I've come across. There is an entire "You Don't Know JavaScript" book dedicated to this exact topic, yet the entire book could not explain it as well as you just did in one sentence...

    • @1998charan
      @1998charan 4 роки тому

      How about the (this & Object prototypes) portion of "You Don't Know Javascript"?
      Till that part stands Great. What is your opinion?

    • @Jimmy-vx7mk
      @Jimmy-vx7mk 4 роки тому

      @@1998charan dai ala

    • @1998charan
      @1998charan 4 роки тому

      @@Jimmy-vx7mk you came to see 'this' it seems :)
      Intha mottaiyan nalla solluvan

    • @Jimmy-vx7mk
      @Jimmy-vx7mk 4 роки тому

      @@1998charan, my dumb ass still can't understand it.

    • @nyxmediaentertainment8723
      @nyxmediaentertainment8723 4 роки тому

      Yeap! I felt it too! Very well put!

  • @Mattou2812
    @Mattou2812 5 років тому +7

    You are a very good teacher. now i understand the basics of this thanks to you. I love your Javascript videos I've learned a lot of things thanks to you. Thanks a lot for sharing your knowledges.

  • @antonmariadas7747
    @antonmariadas7747 3 роки тому +7

    Best explanation ever. Simple, concise and very clear.

  • @siddharthmagadum16
    @siddharthmagadum16 3 роки тому +2

    I heard this channel a lot. By watching THIS first video, I got to know THIS is an awesome channel . Crystal clear explanation. Thanks a lot. Subscribed!

  • @flaviohenrique5294
    @flaviohenrique5294 2 роки тому

    You explain so simples and direct that I couldn't understand why I didn't figured out this before. Thanks man, best didactic ever.

  • @sucesssoulman
    @sucesssoulman 3 роки тому +2

    Thank you!!!! your explanation is THE BEST!!!!! I understood the concept. There is nothing difficult when someone explains it as clear as possible.

  • @anifsayed3563
    @anifsayed3563 3 роки тому +2

    This is by far the best explanation of this. I was struggling to understand the concept. Thank you very much.

  • @the_yugandharr
    @the_yugandharr Рік тому

    Bro this stuff is insane. You're an amazing teacher. No doubts left. Thanks a lot Mosh!

  • @MohiyuddinShaikh
    @MohiyuddinShaikh Рік тому +2

    This was one of the most beautiful explanation on "this" topic. Heading out to solve some quizzes based on this concept to solidify my learning.

  • @stephen1569
    @stephen1569 Рік тому

    I loved this video. It untied the “this” knot in my head. MANY THANKS !!!!

  • @cowabungatv4159
    @cowabungatv4159 5 років тому +2

    Indeed the best tutorial on this i've seen so far

  • @smilejayden-553
    @smilejayden-553 4 роки тому

    Very thanks for a CLEAR explain about 'this'. Love Mosh from Korea

  • @andrew.schaeffer4032
    @andrew.schaeffer4032 Рік тому

    Bravo. I've watched several videos on the this keyword and this is the best one yet

  • @priyamganguly
    @priyamganguly 3 роки тому

    A small, yet complex, topic made easy through this simple video. Thanks!

  • @Joni67sinix
    @Joni67sinix 3 роки тому

    maaann, I can't explain how much you helped me with this video. congrats for your didactics and thanks a lot!!!!

  • @nizamuddinshaikh3185
    @nizamuddinshaikh3185 5 років тому

    Simple, concise and to the point explanation shown! Thanks for sharing.

  • @BobbyBundlez
    @BobbyBundlez 4 роки тому +2

    ONE MINUTE IN AND HE ALREADY CLEARED UP SO MUCH

  • @deepak8586
    @deepak8586 2 роки тому

    This is where I actually I learnt about this!!! thank you so much none in my college explained like this about this!

  • @denisatanase7548
    @denisatanase7548 4 роки тому +1

    Now I really get it omg...also with the 'new' keyword, many thanks!

  • @domenicocucinotta2720
    @domenicocucinotta2720 3 роки тому

    First Time I really understood the this keyword! Thanks Mosh,you are a 🌟

  • @1vigneshram
    @1vigneshram 3 роки тому

    I really loved it mosh!!! very confused before now got clear understanding of how this works with practical knowledge

  • @idhamhafidz
    @idhamhafidz 4 роки тому +3

    I finally understand the part where you add this after callback funtion

  • @kushalkumar4970
    @kushalkumar4970 4 роки тому

    Wow! You just taught me in first 1 min which I needed the most. Thanks a ton!

  • @harshitkhanna7688
    @harshitkhanna7688 5 років тому

    That was a good short explanation. Thanks Mosh.

  • @m.smanoj4688
    @m.smanoj4688 3 місяці тому

    AWESOMELY EXPLAINED - EVEN PAID COURSES CANNOT COME NO WAY NEAR THIS KIND OF EXPLANATION LIKE THIS

  • @haythamkenway8343
    @haythamkenway8343 4 роки тому

    This is the best explanation of 'this' keyword. Thanks a lot mosh 🙂

  • @drakecoleman9364
    @drakecoleman9364 3 роки тому

    The first part of your video is th goal for every tutorial out there. And when you made those claims in the begining that you would have the best way to explain it and understand, that inherently put a lot of pressure on you from me, since this video was a quick search to go over it for a different bigger part of my learning, and this is something thats been confusing me..
    Well, tbh with you.
    You succeeded.
    Well Done man. You did amazing explaining it, and I am in your debt.

  • @user-eu7bh5mt5s
    @user-eu7bh5mt5s 5 років тому

    Thank u a lot Mosh. That was a very clear and good explanation. Helped me a lot with something that was quite unclear to me up to this point :)

  • @machine352
    @machine352 2 роки тому

    Very good video, alem de ter entendido melhor o uso do this i felt happy that I understood practically everything even though I was a Brazilian student of English. Thanks for the class

  • @mdirshath4174
    @mdirshath4174 2 роки тому

    Thanks for the clear explanation. You really explain complex topics easily understandable. Thank you so much!!!

  • @dobromirkrastev7426
    @dobromirkrastev7426 4 роки тому

    Excellent explanation. Thank you, Mosh!

  • @therealorberon
    @therealorberon 6 років тому +2

    I need to watch and re-watch this a few times. to get and remember it.

  • @naumanraees2932
    @naumanraees2932 3 роки тому

    Mr. Mosh you are really great sir, your teaching style, explanation and your knowledge helped me a lot to clear my ideas

  • @kevinmuchene8660
    @kevinmuchene8660 2 роки тому +2

    Excellent video. By using arrow function ie this.tags.forEach(tag => console.log(this, tag)) works. This is because an arrow function doesn't have its own this value. Instead, it uses this value of the enclosing lexical scope.

  • @note5819
    @note5819 4 роки тому

    great video, thx!
    what coding environment were you using? is is an online one like jsfiddle or a os X app

  • @fidtran4991
    @fidtran4991 4 роки тому

    Thank you so much for this lecture. I watched a bunch of videos but still got confuse. With your clear clarifications I feel much more confident learning programming!!!

  • @PoetNoPoems
    @PoetNoPoems 5 років тому +1

    Says it’s complicated from poor teaching material then minute one of his explanation and I’ve got it. Top sh#t mate, thanx

  • @hadircadumitru760
    @hadircadumitru760 4 роки тому

    thank you for a clear explanation of everything, and good English accent

  • @faisalahmed9434
    @faisalahmed9434 4 роки тому

    Mosh is always a good teacher!!

  • @Aarmaxian
    @Aarmaxian 5 років тому

    Thanks. A very easy to understand and abundantly clear video.

  • @Mu-tp6es
    @Mu-tp6es 5 років тому +10

    so basically this references the parent of wherever its called unless its called in an ordinary function then it references window

    • @RickyRicheRebelle
      @RickyRicheRebelle 5 років тому +2

      with many MANY exceptions, such as with callbacks, anonymous function expressions, arrow syntax etc etc... still very complicated

  • @GuitarreroDaniel
    @GuitarreroDaniel 3 роки тому

    Wow, this was so well explained. Thank you very much!

  • @alessandroporfirio1910
    @alessandroporfirio1910 6 років тому

    Thank you for this lecture! It was great!
    Btw, are there a relation between the global object and the global scope besides that both are global? Is the global scope a property of the global object?

  • @2010chalupa
    @2010chalupa 9 місяців тому

    Great explanation!!! Learning Typescript here and needed a clear explanation like this one!

  • @guycohen4403
    @guycohen4403 Рік тому

    Thank you for the last part with the foreach, I didn't know that callback functions behave like that I didn't know how to fix it until now

  • @smrutikantnayak3652
    @smrutikantnayak3652 3 роки тому

    Mosh, literally i was laughing while the console.log logged this keyword inside the call back.... A great 8 and half minutes spent.. Thank you...

  • @itsthomasYT
    @itsthomasYT 6 років тому

    Mosh, damet garm. Hichwaght fekr nemikardam ke behtarin moalleme donya ye Hamwatan bashe. Merci. Zemanan man ta hala 2 ta az coursato kharidam.
    I wish you could create a course about JavaScript Debugging for a real world project. There is nothing like this any where on the web.

  • @akshaybhatt2136
    @akshaybhatt2136 Рік тому

    This is by far the best and the simplest video on 'This'

  • @albertofabbri1543
    @albertofabbri1543 4 роки тому

    Very nice and clear explanation. Thank you very much!

  • @geralddarkobekoe
    @geralddarkobekoe 5 років тому +1

    Thank you a bunch
    It helped me a lot

  • @narekmalkhasyan9636
    @narekmalkhasyan9636 4 роки тому

    Thank you very much. The easiest explanation for --this--
    keyword.

  • @rossjubic8567
    @rossjubic8567 3 роки тому

    Wow This was the best explanation I have come across. I still had to take it slow through the video, but yes This all makes sense now. Hitting that subscribe!

  • @katkodes6055
    @katkodes6055 Рік тому +1

    I've been trying to understand this for over 48 hours and I finally get it!! Thanks :)

    • @alcestabyss3305
      @alcestabyss3305 Рік тому +1

      Hi, from which sourse are you learning java script? 😊

    • @katkodes6055
      @katkodes6055 Рік тому +1

      @@alcestabyss3305 mainly the Odin project and published books

  • @damian1310
    @damian1310 3 роки тому

    I learned something new today. I haven't idea that the second param of the forEach function could add the this keyword!

  • @elgary9074
    @elgary9074 3 роки тому

    Well explained!. Thank you Mosh!

  • @ailanj8221
    @ailanj8221 6 років тому

    great explanation, and by the way, can I know what IDE are you using?

  • @CheeCarniel
    @CheeCarniel 10 місяців тому

    The best so far... clearly understood.

  • @bibekkhatri
    @bibekkhatri 3 роки тому

    Mosh! your explanation is clear and concise.

  • @PieceOfInternet
    @PieceOfInternet Рік тому

    Great video!!!
    what the vscode color theme are you using? looks great

  • @atlasmaxima1418
    @atlasmaxima1418 5 років тому +3

    Wow, I watched only the first minute and it makes so much sense now than trying to read articles about it!

    • @iLoveTurtlesHaha
      @iLoveTurtlesHaha 5 років тому

      LMAO, I was trying to read a stupid article as well before leaving it and coming here. XD

    • @serranomorante
      @serranomorante 5 років тому

      so true.

  • @michaelrosenfeld8055
    @michaelrosenfeld8055 2 роки тому

    Thank you so much for this. One simple sentence and it somewhat clicked for me.

  • @joeyschmitz6882
    @joeyschmitz6882 5 років тому

    Awesome explanation. Thanks!

  • @dingdong8704
    @dingdong8704 4 роки тому

    at 4:54 why u used tag in the call back function does the tags.forEach method passes the array into the argument in the call back function.

  • @aeris-fy6os
    @aeris-fy6os 6 років тому +11

    Finally! My question has been answered! Thanks Mosh!

    • @aeris-fy6os
      @aeris-fy6os 6 років тому

      Programming with Mosh Maybe make a video about best practices in writing Javascript code :)

  • @emmanuelagwu2953
    @emmanuelagwu2953 21 день тому

    Thank you so much for this explanation. it makes the concept of the "this" keyword very clear

  • @abebazouie5672
    @abebazouie5672 3 роки тому

    Damn!!! That was so straightforward. Thankssss!

  • @musafurkankeskin8411
    @musafurkankeskin8411 Рік тому

    Great, brief and simple loved it 👍

  • @nadhiffuadi
    @nadhiffuadi 2 роки тому

    Amazing! Thank you MOSH! and.... may i know where the next lecture is?

  • @amirsaeidi7136
    @amirsaeidi7136 3 роки тому

    Mosh! thank you. It could not have been explained simpler than THIS. :)))

  • @alagappank1242
    @alagappank1242 2 роки тому

    Only video in you tube that clears the "this" keyword concept💥

  • @janescookinglab
    @janescookinglab 4 роки тому

    Thank you! This is very helpful!

  • @pannihto7588
    @pannihto7588 4 роки тому

    Thank you. It's much more clear now

  • @shivarammuthukumaraswamy7164
    @shivarammuthukumaraswamy7164 3 роки тому

    Amazing explanation.Thank you so much.

  • @davidtran1360
    @davidtran1360 2 роки тому

    Great video. I fully undertand "this" now

  • @80Vikram
    @80Vikram 3 роки тому +8

    7:20 you've mentioned that's topic for next lecture. Please clarify where can I find "next lecture" ?

    • @Snoo29293
      @Snoo29293 3 роки тому +3

      I don't know what exactly he was going to include in that lecture, however, for those curious to find out a way to solve that problem, from what I know, you could simply use arrow function instead of normal function, arrow function treats this differently than normal functions, that's the main reason arrow functions of ES6 are so cool, one advice I have in order to avoid errors in complicated codes is to use classes for object constructors, functions in the global scope and for object.prototype properties and use arrow functions everywhere else.

  • @katiushka22
    @katiushka22 4 роки тому +1

    THIS was driving me crazy until I watched your video. Thanks!!!

  • @tyrodev5281
    @tyrodev5281 2 роки тому

    Now "this" is what I came for! Thanks Mosh!

  • @atulcodex7730
    @atulcodex7730 5 років тому

    Best video to understand this keyword thank u :) Mosh

  • @exploreasanaussiemillenial
    @exploreasanaussiemillenial 4 роки тому

    You Sir, are a champion, Thank You!

  • @vancouverhp90
    @vancouverhp90 2 роки тому

    This is the best explain for This keyword i've ever seen :) Thank you so much ....

  • @dankquan743
    @dankquan743 4 роки тому

    Excellent Video Mosh. Can we get one like this on Bind, Call, and Apply?

  • @ghosthail
    @ghosthail 4 роки тому

    Great video. Such a clear explanation.

  • @arthurnino93
    @arthurnino93 4 роки тому

    Favorite tutor mentor in programming

  • @ore_bear8045
    @ore_bear8045 3 роки тому

    superclass, this explanation of this is the best the world has ever seen.

  • @yeeli7989
    @yeeli7989 5 років тому +236

    This is complicated

    • @jsonkody
      @jsonkody 5 років тому +4

      No it really is not. Learn more basic stuff. Then make some things. Then come back again, you'll see ;)

    • @Anniek62
      @Anniek62 5 років тому +63

      explained to my 7 year old nephew. he died

    • @frugglewiggle
      @frugglewiggle 5 років тому +1

      nice pun

    • @sarangcheruvattil5762
      @sarangcheruvattil5762 4 роки тому

      @@Anniek62 LMFAO :D :D :D

    • @BobbyBundlez
      @BobbyBundlez 4 роки тому

      @@Anniek62 LMFAOOO

  • @user-nj4sw9io1h
    @user-nj4sw9io1h Рік тому

    Thanks for this simple and understandable video.

  • @ingridblumenfeld8299
    @ingridblumenfeld8299 4 роки тому

    To me, you're like Forrest Gumps' momma. You always have a way of explaining things so I can understand them. THANK YOU