The Complete JS Notes #5 🧵

Baris Balli
4 min readJul 1, 2022

Welcome to the fourth article of my JavaScript notes.

In this article we will talk about: this in Functions, Primitive vs Object, Object.assign()

All the content is taken from Jonas Schmedtmann’s amazing course The Complete JavaScript Course 2022: From Zero to Expert!

Please purchase the course to understand the content, this is simply my summarized notes of the course.

In the arrow functions something really weird happens it doesn’t have its own this so it takes it from the parent that’s why we got this bug in our code.

In the code, it didn’t return jonas.firstName it returned window.firstName which does not exist.

Whenever we try to reach a not-existed property of an object we don’t get an error we get *undefined* .

Even worse if we have a var firstName in our global scope it returns that and it becomes a quite complicated error to solve.

Lets say we created an irregular function inside a regular function. Can we use this in the irregular function

It looks like we should use it because even though it doesn’t have its own this regular function has it. But no my friend js doesn’t allow an irregular function to have this. If we put it in the global state same thing would happen.

There is solution to this problem however

Using a variable to hold the value is the solution. This is what js devs did in good ol’ days.

Today we have a sexier solution. Arrow Functions

Arrow functions doesn’t have their own this keyword so they directly inherit from the parent scope and it solves the problem.

Just like this keyword only regular functions has access to arguments keyword

Arguments in an array of parameters and it actually allow us to write more arguments than we specified in the function. Crazy shit huh!

Arguments don’t work in other functions

Primitives vs objects

First we checked primitives, after copying the value they have nothing to do with each other.

But in objects even though my frind changed its age I am being effected. This is because of pointer logic in objects.

Even though we defined objects with const we could change them because their address in the heap memory doesn’t change!.

In the call stack values are immutable they can’t be changed so primitive values should go to a new address to do this.

How to copy an object then?

Object.assign merges 2 objects and if we merge it with an empty object we have a totally new object.

However we have a problem with this method Object.assign only copies variables in the first level if object has another object inside it we can’t use it. This is called shallow copy but we need a deep copy

Making deep copy is very complex we need an external library, so it is not going to be covered in this section.

Follow me on twitter 💣🔥

--

--