One of the required prerequisites to learn React is ๐๐ฆ๐ฒ.
๐๐๐ ๐ โ๐๐ข๐๐ ๐๐๐๐ค ๐ธ๐6 ๐คโ๐๐กโ๐๐ ๐ฆ๐๐ข'๐๐ ๐๐๐๐๐๐๐๐ ๐
๐๐๐๐ก ๐๐ ๐๐๐ก.
๐งต This thread is all you need to know about ES6 before you get your hands on React:
#100DaysOfCode #javascript
โ๏ธ You'll be able to learn React way better if you know working with ES6.
ES6 is a scripting language for JavaScript and helps developers by making the code simple as it handles out all the limitations of the main language (JS).
๐
๐ญ. ๐ฆ๐ฐ๐ผ๐ฝ๐ฒ ๐ผ๐ณ ๐ฉ๐ฎ๐ฟ๐ถ๐ฎ๐ฏ๐น๐ฒ๐ ๐ถ๐ป ๐๐ฆ
Before moving to the point, let us understand 3 concepts here:
1๏ธโฃ Global Scope - Variable is declared outside the function. This variable is accessible inside every function present in the code.
2๏ธโฃ Function Scope - Variable is declared inside (within) a function, outside that it is not accessible anywhere.
3๏ธโฃ Block Scope - Block scope means variables that are declared in a { } block are not accessible outside it. This block can be an if statement, for/while loop..
๐ฎ. ๐น๐ฒ๐/๐ฐ๐ผ๐ป๐๐ ๐ผ๐ฟ ๐๐ฎ๐ฟ?
- var: function/ global scoped
var is both global and function scoped, which often creates a confusion. So avoid using it.
- let: block scoped.
โ let keyword can be reassigned:
But when we use let inside a function, it works like:
Inside the function box() when we log the value of size, it shows a reference error. That is because let is block scoped.
- const: block scoped
โ const is very similar to let except that they can't be changed and redeclared.
โ therefore let and const are preferred over var keyword for declaring variables.
๐ฏ. ๐ข๐ฏ๐ท๐ฒ๐ฐ๐๐
-> objects are written within curly braces { } as a collection of key: value pairs.
key: property name
value: value of that property
- Creating an empty object:
Talking specifically about ES6, before ES6 we had to specify both (key, value) even if both are of the same names.
ES6 helps us to get rid of duplication when we have the same key: value names. So now our code will look like this:
๐ฐ. `๐๐ต๐ถ๐` ๐ธ๐ฒ๐๐๐ผ๐ฟ๐ฑ
this is a keyword. It basically returns a reference to the object it is placed within
๐ก NOTE:
When we call a function as a method in an object, this keyword returns a reference to that object. ๐
But when we call the function alone, outside the object this returns the global object (browser window) and hence we get the result as undefined ๐
๐ฑ. ๐๐ฟ๐ฟ๐ผ๐ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐
- Helps us to write functions in a much shorter, concise, and easier way.
- Having a single argument can remove the () around our parameters (num in below case)
๐ฒ. ๐ข๐ฏ๐ท๐ฒ๐ฐ๐ ๐๐ฒ๐๐๐ฟ๐๐ฐ๐๐๐ฟ๐ถ๐ป๐ด
Let's say we have an object called girl such that it has 3 keys such as:
const girl = {
name: " ",
age: " ",
country: " ",
};
Normally, we would do something like this to get the values:
here, as you can see we have to repeat the object name girl every time we want to get a value. This problem can be solved by object destructuring:
๐๐๐๐ ๐ก { ๐๐๐๐, ๐๐๐, ๐๐๐ข๐๐ก๐๐ฆ } = ๐๐๐๐;
this one-line code works the same as the previous code.
So destructuring made our code shorter and easier to understand.
In case you want to use an alias (a different variable name) instead of key:
๐๐๐๐ ๐ก {๐๐๐ข๐๐ก๐๐ฆ: ๐๐ก๐๐ฆ} = ๐๐๐๐;
- This above line of code means we've defined a new variable called ctry = country.
๐ณ. ๐ฆ๐ฝ๐ฟ๐ฒ๐ฎ๐ฑ ๐ข๐ฝ๐ฒ๐ฟ๐ฎ๐๐ผ๐ฟ
- We use spread operator for adding items to an array, combining arrays/ objects, merging or cloning and array, and many more. Let's have a quick look at each of them:
Let's say we have two arrays such that
Case 1 - ๐ด๐๐๐๐ฆ๐
๐ถ๐ด๐๐ธ ๐ผ๐ผ - ๐๐๐๐๐๐ก๐
๐ด. ๐๐น๐ฎ๐๐๐ฒ๐
-> they behave as a template for creating objects
Let us take an example of an object boy here. We have a function called run inside it.
Now if we've some bug in the future or we've to modify our function for a different object, it would be a long way.
- To overcome this and make our work easier, we use classes:
*Always add the constructor method when creating objects*
Now that we've created a class, let's try building our object again -
see it's that simple!
with this above class, we've implemented the run method in a single line of code. If someday we find a bug here, we've to modify it in just a single place {inside class Boy}. So this is the advantage of using classes in JS.
Now let's talk about Inheritance..
๐ต. ๐๐ป๐ต๐ฒ๐ฟ๐ถ๐๐ฎ๐ป๐ฐ๐ฒ
-> when we acquire properties of one class into another class to extend it, it is inheritance.
If we have a class Boy such that -
..and we want to create another class (having similar properties + some specific properties of its own). We can do this using the keyword ๐๐ฅ๐ก๐๐๐๐
we just created the class Girl here. Let us now create a const using this -
๐๐๐๐ ๐ก ๐๐ฆ๐บ๐๐๐ = ๐๐๐ค ๐บ๐๐๐("Sara");
and we're done. This code basically means that now the const myGirl will have the functions eat + run + constructor property of Boy class.
Now we can use it like below without writing a long piece of code:
myGirl. eat()
myGirl. run()
Now let's say we want to create another constructor inside the Girl class (derived class constructor).
We must call ๐ ๐ข๐๐๐() constructor inside the new constructor, otherwise, we'll get an error. Now, this must be looking confusing, let's look at the example below -
๐ญ๐ฌ. ๐ ๐ผ๐ฑ๐๐น๐ฒ๐
At times, we can have many no. of classes declared in a single file. This makes the code long, confusing and messy.
To avoid this, we separate these classes into different files and import them as a module into the main file.
This is called modularity.
Let's look at it in action. Here's what our folder ๐ ๐๐ will look like:
Here we created two files (boy, girl) inside ๐ ๐๐ folder.
Doing this simple setup can make our code much easier to understand!
Why we used ๐๐๐๐๐๐ก and ๐๐ฅ๐๐๐๐ก? ๐
both Boy and Girl classes are private in the folder, in order to use them we made them public using the ๐๐๐๐๐๐ keyword.
*We use ๐๐๐๐๐๐ก keyword in line 1 of girl.js as it is an extended version of the Boy class.*
Now half of the work is done. For now, these classes are not accessible in our main app.js file. For that, we've to import them into our app.js file. We can do that by using -
๐ญ๐ญ. ๐๐ฒ๐ณ๐ฎ๐๐น๐ ๐ฎ๐ป๐ฑ ๐ก๐ฎ๐บ๐ฒ๐ฑ ๐๐
๐ฝ๐ผ๐ฟ๐๐
- Named Exports
We can export more than one object from a specific module. This is called named export. Eg:
Here we exported a class `Car` and a function `add`.๐
- Default Exports
It is basically the main object that is exported from the module. It is generally used in case we've only a single object to export. Let's see how it is -
๐ก Now we don't need the import { Car } from "./car";
Instead, we use import Car from "./car"; in case of default exports.
I know this is too long to be called a thread and can be difficult to read, so I've compiled it in a blog. You can read it here:
dev.to/shreya/es6-hanโฆ
๐ Woosh! You've made it to the end. Hope I've helped you somehow.
If it did, ๐ and โค๏ธ the thread and let's connect @eyeshreya :)
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.