Betwixt Code and Music

What Is This?

September 15, 2015

JS Keyword “this”

Ok, things are about to get confusing. If you are not thinking clearly then go skip this and go read the facebooks or something. Maybe go take a walk.

Still here? Well, let’s talk about this

i-don't-like-this

Just kidding. This is awesome. The easiest way to put this…erm, what I am trying to say is…the best way to get this down on paper…blurgh… Using this word seems nearly impossible!

In JavaScript we are often dealing with a small snippet of code that does a task. It is called a function. Inside that function we might be targeting a specific item that triggered an event. A shorthand way of talking about some information that is passed in to the function is to use the this keyword. It enables the programmer to talk about a generic piece of information without knowing exactly what it is ahead of time.

Specifically, this refers to the current DOM element, at least in the way that we have been using it in class. It is also useful when creating new objects with a constructor. I have not used it a ton just yet (only a couple of times), so I have yet to build up a lexicon of knowledge to disseminate to the masses about this.

ron-burgundy-immediately-regrets-this-decision

If you want to learn more about this, then go to MDN or just !google search it on DuckDuckGo.


Michael D. Mathew.

Written by Mike Mathew who lives and works in Dallas building useful things. Sometimes he tweets.