Savvas Stephanides Profile picture
Sep 21, 2020 15 tweets 3 min read Read on X
What is "this" in Javascript? Let's explain:

Thread 🧵👇

#100DaysOfCode #CodeNewbies
First off, a note: I was planning on create a thread like this for ages, but I have been putting it off for one simple reason: just like a lot of people, I too didn't quite get the "this" keyword. It still baffles me a bit but hopefully this basic walkthrough will make sense.
"this" is basically a label. It's a label you put on a function to show who it belongs to. Sort of like putting a label on your food at work saying "THIS SANDWICH BELONGS TO <your name>"
So what do I mean by "belongs"? Well, take a look at this code which shows a function defined within another function:

function doSomeStuff(){
return {
doSomeRelatedStuff: function(){

}
}
}
Take a look at the doSomeRelatedStuff() function. It's a function that does some stuff but who does it belong to?

It belongs to the function above it: doSomeStuff()
Now take a look at this, which defines a function within a function within a function:

function doSomeStuff(){
return {
doSomeRelatedStuff: function(){
return {
doOtherStuff: function(){
}
}
}
}
}
Now have a look at doOtherStuff(). Which function does it belong to?

Once again, it belongs to the function defining it: doSomeRelatedStuff()

👉 This is what "this" points to.
Therefore, running this code:

function doSomeStuff(){
return {doSomeRelatedStuff}

function doSomeRelatedStuff(){
console.log(this)
}
}

doSomeStuff().doSomeRelatedStuff()

Will print the doSomeStuff() in the console.
But what if the function calling "this", doesn't belong to any function? Example:

function doStuff(){
console.log(this)
}
In this case, "this" returns what is called the "global object". In the browser, the global object is "window". In Node, it's "global".
The global object is basically contains the functions you can call without mentioning that object first. For example, "window.location.href" can be called without mentioning "window": "location.href".
OK, so what do we do with the "this" keyword?

Using "this" we have access to the parent object's properties.
Example:

function doStuff(){
return {
name: "Ben",
printName: function(){
console.log(this.name)
}
}
}
In the code above doStuff() has a variable called "name" and a function called "printName". How can "printName" get the name to print it? It needs to get it from its parent object, "doStuff()". This is easy, because the "this" keyword can give "printName()" access to "doStuff()"!
In a VERY basic way, this is how "this" works in Javascript. At least in the context of functions. I hope it makes sense a little bit more. There is more to it of course, which you can learn here developer.mozilla.org/en-US/docs/Web…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Savvas Stephanides

Savvas Stephanides Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @SavvasStephnds

Jan 23, 2023
What are Data Structures? 💿

Let's explain.
When you use a website or an app, a lot of things are going on in the background in order to give its users what they're looking for.

Things are being carried out and the result of these things depend on other things.
One of these things that influence how an app behaves, is the information that it has on hand. For example:

💿 The user's name so the app can give a personalised greeting.
💿 The list of tasks in a to-do list so the app can list them, add things or remove things from it.
Read 12 tweets
Sep 27, 2021
Javascript is the most versatile programming language out there.

You can build anything: interactive web apps, to servers, to mobile and desktop apps.

It does come with its own set of concepts that anyone working with it should know.

Here's 11 of them, explained.

👇🧵
Promises, explained:

savvas.me/explained/prom…
async/await explained:

savvas.me/explained/asyn…
Read 12 tweets
Sep 25, 2021
TOP 5 CHEST PRESS EXERCISES!

Thread 🧵👇 #FitnessWeekend Image
5. MACHINE CHEST PRESS

Equipment: machine

How to do it: bodybuilding.com/exercises/leve… Image
4. CABLE CROSS-OVER

Equipment: cable

How to do it: bodybuilding.com/exercises/cabl… Image
Read 8 tweets
Jun 3, 2021
WHAT IS GRAPHQL?

Let's explain!
This thread is also available in a more readable and accessible format here:

🔗 savvas.me/explained/grap…
You probably heard of GraphQL as a new way to retrieve data from a web service. Possibly as an alternative to REST services. But what is it exactly?
Read 16 tweets
Jun 1, 2021
How I made savvas.me

A thread 🧵👇 Image
1⃣ The stack:

I wanted my website to be as fast and snappy as possible.

Which is why this website is 100% Jamstack. The code, posts, assets are all stored in a repository on Github - github.com/savvasStephani….

...and the website itself is made with Gatsby. Image
2⃣ Hosting

For my website, I wanted for the builds to execute immediately after pushing new changes.

The Markdown blog files, the assets (images, videos), JS files, styling etc are all hosted on Github.

The end result from building the website is hosted and managed on Netlify. Image
Read 9 tweets
Mar 29, 2021
Should I use forEach() or map()?

Both iterate through arrays in Javascript, but how do they differ? Let's see:

Thread 🧵👇

#100DaysOfCode #CodeNewbie #javascript
🚨 This thread is also available in a more accessible version here:

👇

🌐 savvas.netlify.app/explained/fore…
If you're learning Javascript, sooner or later you'll come across the forEach() and map() functions. It is quite often that there is some confusion about what each one does.
Read 15 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(