TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How do you explain "this" to beginners?

3 pointsby bhaumikabout 11 years ago

4 comments

DonHopkinsabout 11 years ago
In JavaScript, &quot;this&quot; is not a variable, or a binding in the local scope. It is a keyword, so it is magic. It was a terribly idiotic design decision, because the variable you&#x27;d want to close over most of the time is &quot;this&quot;, but since it&#x27;s not a variable, you can&#x27;t close over it -- it&#x27;s dynamically bound, in a really stupid way.<p>It&#x27;s the cause of a lion&#x27;s share of JavaScript bugs. No matter how well you know JavaScript, and how pedantically you understand how &quot;this&quot; works and why it&#x27;s such a terrible design flaw, you will ALWAYS make mistakes with it, and you will ALWAYS have to go carefully over your code line by line looking for misuses of &quot;this&quot;.<p>Whenever your JavaScript program is misbehaving, the first thing you should do is to search the relevant code for &quot;this&quot;, and think carefully about how it might be bound. And even if your JavaScript program is not misbehaving in a way you can detect, you should still do that anyway, because it might just have some very subtle bugs caused by &quot;this&quot;.<p>I&#x27;ve been writing code that starts out with &quot;var self = this;&quot; and then ONLY using &quot;self&quot; and NEVER using &quot;this&quot; except for the first line of a function that goes &quot;var self = this;&quot;. That way there&#x27;s a lot less chance of making mistakes, although it&#x27;s still possible for that first &quot;this&quot; to be incorrectly bound -- you only want to do the &quot;var self = this;&quot; in top level functions, and all functions contained in them should use self and never use &quot;this&quot;.
brianchuabout 11 years ago
It&#x27;s whatever is to the left of the dot.<p>The exceptions:<p>1) when used with new, it&#x27;s the new instance of the object (pretend that when you use the new keyword, an Object.create() call is inserted in the beginning of the constructor function),<p>2) when there&#x27;s nothing to the left of the dot, it&#x27;s the window object.<p>3) .call and .apply force this to be something else.
评论 #7678225 未加载
评论 #7680903 未加载
MarkyPc3about 11 years ago
If the beginner understands functions I tell them it&#x27;s the specific object running the function call. If not, I tell them it&#x27;s the object that owns the most immediate scope (as in the one &quot;running the code&quot;). Also important here is the definition of what an object is. The key sentence to memorize is: &quot;An object is an instance of a class&quot; which is to say that the two are not the same. Saying a class is a blueprint and an object is the actual house that was built according to the blueprint (the class definition)is a good way to get this through. &quot;This&quot; in the above analogy more easily means a reference to a specific house.
评论 #7678236 未加载
kogirabout 11 years ago
I&#x27;m pretty sure that the language in use matters here. Care to specify?<p>For example, the answers for JavaScript and C# are not the same.
评论 #7676788 未加载