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

Keeping Track of "This" in JavaScript

475 ratings | 32917 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 (25)
Myles Gmail (6 months ago)
Great video, thx!
Arijit Roy (10 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 (1 year ago)
Well Explained!!
Elati Sheshanth Reddy (1 year 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 (1 year 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 (1 year 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 (2 years ago)
I recommend not covering the code with "the results". It makes it difficult to look back and forth and compare.
gpicard1 (2 years ago)
leave it to o'reilly to make things even more confusing
sumant kanala (2 years ago)
'This' is interesting!
kumar kumaresh (2 years ago)
nice one
T.C. THOMPSON (2 years ago)
Excellent video. Just want a banana now....
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 (5 years ago)
'this' is a pointer to the actual object.
triforcelink (5 years ago)
'this' is a lot similar to 'self' from other languages like python.
Mudit Jha (7 months 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 (2 years 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. (5 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.