Javascript : this, localStorage, and addEventListener

  1. this

In Javascript, this is a keyword that refers to the object which a function was called on.

If I were to run


with no current object, it would return an alert with [object Window]

2. localStorage

First we should understand what cookies are, they’re files that a website puts on your hard drive that holds user information such as options checked, text inputted, date data, and many more. When you re-open a website, the website reads that file which is how logins work and why you might find yourself logged out of a website you’ve been logged in for a while, because the file is either deleted or expired. They unfortunately have a downside of slowing down your browser, sending unencrypted data and limited to 4 KB.

Enough about cookies, lets talk about localStorage

localStorage is a native feature on all browsers that stores named key/value pairs on your device, it is similar to cookies in a way but it doesn’t send any data to the server, has a much larger size of 5 MB, and doesn’t impact your browsers performance

3. addEventListener

addEventListener is a method that attaches an event handler on an EventTarget. The difference between addEventListener and onclick is that addEventListener doesn’t override any existing handlers whereas onclick does, also addEventListener doesn’t work on any Internet Explorer versions before 9, whereas onclick does.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.