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
"This" was the first word I said as a baby. I can't believe that I'm still stuck here...
lol
Did figure it out what exactly is it?? Plaeseeeeeee explain me in simple words😭😭
@@sohailaali2081 The keyword ''this'' is basically an object that is binded to the function that is being executed.
@@CapeSkill yeah thanks but I already had figured it out last month😂😂❤️
@@sohailaali2081 It's not really as difficult as people make it seem to be.
"this" ain't stopping me from going forward in my path.
" 'this' references the object that is executing the current function" ..simple, yet enlightning.. thanx!
Thanks! This saved me from a lot of confusion
Me too commotion solved
“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.
This simple line explains the entire concept
It's not that simple you have to know it's properties where to implement it..
THIS is where I quit programming.
I can feel you. Not really noob friendly but you can do it!
first watch this, It will help you ua-cam.com/video/n_9oUP1GQz0/v-deo.html
How are you on every single video I watch?
😂😂nice comment
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
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.
because arrow function creates a closure, it's similar to
var that = this
this.tags.forEach(function(tag) {
console.log(that.title, tag)
})
@@marvinalone man your keywords tags that this really confused me
😢😢
still could not manage to explain to my 7 yrs old brother.
hahaha
i don't know about your brother but I'm 24 and I'm fucked
@@mohammaddh8655 same hhhhhhhhhhhhhhhhhh
What a dumbass. A 7 year old struggling to understand this.
i tried to explain this to my 7 year old brother, then i realized i don't have one.
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.
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)
honestly, just use an arrow function instead of inserting "this"
@@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...
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)
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!
this!
I totally second this! New sub here!
Without you, I wouldn't have learnt Angular. You're the best lecturer out there Mosh...
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!
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.
you confused about what i had learnt till now, thanks to me for reading your comment.
Can somebody summarize this comment further for me plss
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
"THIS" is all summary that I want for enlightened myself. Thank you!
thanks
"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.
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...
How about the (this & Object prototypes) portion of "You Don't Know Javascript"?
Till that part stands Great. What is your opinion?
@@1998charan dai ala
@@Jimmy-vx7mk you came to see 'this' it seems :)
Intha mottaiyan nalla solluvan
@@1998charan, my dumb ass still can't understand it.
Yeap! I felt it too! Very well put!
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.
Best explanation ever. Simple, concise and very clear.
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!
You explain so simples and direct that I couldn't understand why I didn't figured out this before. Thanks man, best didactic ever.
Thank you!!!! your explanation is THE BEST!!!!! I understood the concept. There is nothing difficult when someone explains it as clear as possible.
This is by far the best explanation of this. I was struggling to understand the concept. Thank you very much.
Bro this stuff is insane. You're an amazing teacher. No doubts left. Thanks a lot Mosh!
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.
I loved this video. It untied the “this” knot in my head. MANY THANKS !!!!
Indeed the best tutorial on this i've seen so far
Very thanks for a CLEAR explain about 'this'. Love Mosh from Korea
Bravo. I've watched several videos on the this keyword and this is the best one yet
A small, yet complex, topic made easy through this simple video. Thanks!
maaann, I can't explain how much you helped me with this video. congrats for your didactics and thanks a lot!!!!
Simple, concise and to the point explanation shown! Thanks for sharing.
ONE MINUTE IN AND HE ALREADY CLEARED UP SO MUCH
This is where I actually I learnt about this!!! thank you so much none in my college explained like this about this!
Now I really get it omg...also with the 'new' keyword, many thanks!
First Time I really understood the this keyword! Thanks Mosh,you are a 🌟
I really loved it mosh!!! very confused before now got clear understanding of how this works with practical knowledge
I finally understand the part where you add this after callback funtion
Wow! You just taught me in first 1 min which I needed the most. Thanks a ton!
That was a good short explanation. Thanks Mosh.
AWESOMELY EXPLAINED - EVEN PAID COURSES CANNOT COME NO WAY NEAR THIS KIND OF EXPLANATION LIKE THIS
This is the best explanation of 'this' keyword. Thanks a lot mosh 🙂
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.
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 :)
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
Thanks for the clear explanation. You really explain complex topics easily understandable. Thank you so much!!!
Excellent explanation. Thank you, Mosh!
I need to watch and re-watch this a few times. to get and remember it.
Mr. Mosh you are really great sir, your teaching style, explanation and your knowledge helped me a lot to clear my ideas
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.
great video, thx!
what coding environment were you using? is is an online one like jsfiddle or a os X app
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!!!
Hlo
Says it’s complicated from poor teaching material then minute one of his explanation and I’ve got it. Top sh#t mate, thanx
thank you for a clear explanation of everything, and good English accent
Mosh is always a good teacher!!
Thanks. A very easy to understand and abundantly clear video.
so basically this references the parent of wherever its called unless its called in an ordinary function then it references window
with many MANY exceptions, such as with callbacks, anonymous function expressions, arrow syntax etc etc... still very complicated
Wow, this was so well explained. Thank you very much!
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?
Great explanation!!! Learning Typescript here and needed a clear explanation like this one!
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
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...
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.
This is by far the best and the simplest video on 'This'
Very nice and clear explanation. Thank you very much!
Thank you a bunch
It helped me a lot
Thank you very much. The easiest explanation for --this--
keyword.
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!
I've been trying to understand this for over 48 hours and I finally get it!! Thanks :)
Hi, from which sourse are you learning java script? 😊
@@alcestabyss3305 mainly the Odin project and published books
I learned something new today. I haven't idea that the second param of the forEach function could add the this keyword!
Well explained!. Thank you Mosh!
great explanation, and by the way, can I know what IDE are you using?
The best so far... clearly understood.
Mosh! your explanation is clear and concise.
Great video!!!
what the vscode color theme are you using? looks great
Wow, I watched only the first minute and it makes so much sense now than trying to read articles about it!
LMAO, I was trying to read a stupid article as well before leaving it and coming here. XD
so true.
Thank you so much for this. One simple sentence and it somewhat clicked for me.
Awesome explanation. Thanks!
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.
Finally! My question has been answered! Thanks Mosh!
Programming with Mosh Maybe make a video about best practices in writing Javascript code :)
Thank you so much for this explanation. it makes the concept of the "this" keyword very clear
Damn!!! That was so straightforward. Thankssss!
Great, brief and simple loved it 👍
Amazing! Thank you MOSH! and.... may i know where the next lecture is?
Mosh! thank you. It could not have been explained simpler than THIS. :)))
Only video in you tube that clears the "this" keyword concept💥
Thank you! This is very helpful!
Thank you. It's much more clear now
Amazing explanation.Thank you so much.
Great video. I fully undertand "this" now
7:20 you've mentioned that's topic for next lecture. Please clarify where can I find "next lecture" ?
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.
THIS was driving me crazy until I watched your video. Thanks!!!
Now "this" is what I came for! Thanks Mosh!
Best video to understand this keyword thank u :) Mosh
You Sir, are a champion, Thank You!
This is the best explain for This keyword i've ever seen :) Thank you so much ....
Excellent Video Mosh. Can we get one like this on Bind, Call, and Apply?
Great video. Such a clear explanation.
Favorite tutor mentor in programming
superclass, this explanation of this is the best the world has ever seen.
This is complicated
No it really is not. Learn more basic stuff. Then make some things. Then come back again, you'll see ;)
explained to my 7 year old nephew. he died
nice pun
@@Anniek62 LMFAO :D :D :D
@@Anniek62 LMFAOOO
Thanks for this simple and understandable video.
To me, you're like Forrest Gumps' momma. You always have a way of explaining things so I can understand them. THANK YOU