henrycode

  • Archive
  • RSS

JavaScript: Clarifying the Keyword ‘this’

Check out the discussion about this blog post on HN:
news.ycombinator.com/item?id=4985853

The keyword ‘this’ holds a value and is bound to another object. It can be one of the most confusing subjects in JavaScript. It doesn’t have to be if you follow this simple rule:
The keyword ‘this’ is bound dynamically to the object found to the left of the ‘.’ at call time.

this example

Call time is when a set of parenthesis () begins the execution of our function. In our case ”person” is to the left of the ‘”.’” at call time. So in the example above, ‘this’ refers to our object person.

There are three exceptions to the above.
1) When there is no ‘.’ the keyword ‘this’ is bound to the global object window.
2) When you use call and apply, you get to decide what ‘this’ is bound to.
3) When you use the keyword new to create a new instance from a constructor, the keyword ‘this’ refers to the newly generated instance.

Special shout out to Marcus who’s a software engineer at Twitter and curriculum designer at Catalyst Class for teaching me how simple the keyword ‘this’  is. All credit goes to him.

    • #this
    • #JavaScript
    • #keywords
  • 5 months ago
  • 19
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

19 Notes/ Hide

  1. theinternauts likes this
  2. zachshallbetter likes this
  3. tumadman likes this
  4. divineflame likes this
  5. crowchick likes this
  6. pdxiii reblogged this from henrycode
  7. pdxiii likes this
  8. daftken likes this
  9. keogh likes this
  10. nicksh likes this
  11. arahmanshah reblogged this from henrycode
  12. arahmanshah likes this
  13. oceanhere likes this
  14. hassan-khan likes this
  15. iamanauthorityonnothing likes this
  16. scotth likes this
  17. codreamers likes this
  18. techphilosophy likes this
  19. henrycode posted this
← Previous • Next →

About

Hi I'm Henry.

I'm a developer @



I hack @

located in awesome San Francisco.

Website
Résumé
GitHub
Twitter
  • RSS
  • Random
  • Archive
  • Mobile
Effector Theme by Pixel Union