HomeНаука и техникаRelated VideosMore From: O'Reilly

Keeping Track of "This" in JavaScript

430 ratings | 31333 views
In Java, the special variable "this" is used to refer to an object. But following which object "this" refers to depends on the code you're executing and how "this" is used. In this JavaScript teaser, we help you keep track of a coding variable more slippery than a banana peel. - Don't miss an upload! Subscribe! http://goo.gl/szEauh - Stay Connected to O'Reilly Media. Visit http://oreillymedia.com Sign up to one of our newsletters - http://goo.gl/YZSWbO Follow O'Reilly Media: http://plus.google.com/+oreillymedia https://www.facebook.com/OReilly https://twitter.com/OReillyMedia
Html code for embedding videos on your blog
Text Comments (26)
Myles Gmail (27 days ago)
Great video, thx!
Arijit Roy (4 months ago)
So I have a question... Which procedure to follow to call a function which is not part of a obj. Sending this as an argument or using .call(). And the must answer question would be.. Why to opt for that solution rather than other one?
vijay kumar M (6 months ago)
Well Explained!!
Elati Sheshanth Reddy (10 months ago)
Excellent tutorial of ```this``` keyword. Googled a lot for a better explanation on ```this``` keyword, but this is the best explanation one can get.
Oscar Diaz (10 months ago)
My sincere and appreciative thanks for giving a pretty thorough explanation of such a mysterious and at times, misunderstood feature in JS.
John Work Feeney (11 months ago)
I want to make sure my students have the materials they need to succeed, so I just created a request for my classroom at DonorsChoose.org: Coding With Spheros. Spheros are programmable robots my coding students will design courses and create code to have to Spheros accomplish various objectives. The link below will provide you with additional information about my students, our class, and why Spheros are necessary for my students. Please consider giving to my classroom by February 14 and your donation will be doubled thanks to DonorsChoose.org. Just enter the code LIFTOFF on the payment page and you'll be matched dollar for dollar (up to $50). If you chip in to help my students, you'll get awesome photos and our heartfelt thanks. Feel free to contact me if you need any clarification or have any questions. Thanks so much, John Feeney P.S. If you know anyone who may want to help my classroom, please pass this along! https://www.donorschoose.org/project/coding-with-spheros/3070833/
Elisandro De Leon (1 year ago)
I feel like that first "banana slip" is more of a scope issue, than a THIS issue.
Benas Milasauskas (1 year ago)
You speaking way to fast about this topic, when there is so many redundant words its really hart to grasp the concept.
Zonayed Ahmed (1 year ago)
What is the difference between methods and functions in JavaScript? Is the method and object same thing here?
Patrick G (1 year ago)
Method is a function within an object. From example below... "myFunction" is a method of "x". var x = { myFunction: function() { console.log('hello") } }
PASS Signs (1 year ago)
I recommend not covering the code with "the results". It makes it difficult to look back and forth and compare.
gpicard1 (1 year ago)
leave it to o'reilly to make things even more confusing
sumant kanala (1 year ago)
'This' is interesting!
kumar kumaresh (1 year ago)
nice one
T.C. THOMPSON (1 year ago)
Excellent video. Just want a banana now....
Zato (1 year ago)
I demand that all explanations use gorillas and bananas metaphors from now on.
Vic W (2 years ago)
One little suggestion: if the cursor is able to point at the words corresponding to the voiceover, it would be more helpful.
Kosmo Bentanour (2 years ago)
R.I.P Harambe
Cuong Nguyen Van Vu (2 years ago)
Thank you so much. I have been struggling in understanding the concept of "this" f. It's solved within just 13 mins by watching this video.
Daredevil (3 years ago)
I can't hear anything is it muted?
Blueprint (4 years ago)
'this' is a pointer to the actual object.
triforcelink (4 years ago)
'this' is a lot similar to 'self' from other languages like python.
Mudit Jha (1 month ago)
this is only similar to Python's self when you're using it from inside a constructor function where this(like self) refers to the newly created object
Daryn (1 year ago)
triforcelink "this" in JS is not the same as "self" in Python. In JS the value of "this" changes depending on how the function was called. You actually can't always tell what the value of "this" is without looking at how the function was called. There are 5 different rules, view the video for examples.
Daredevil (3 years ago)
+triforcelink Why no one said this to me before, they all act like "this" is a very unique feature? ;_;
Göran E. (4 years ago)
This is reffering to the object, as in other programming languages. You are correct :P

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.