Why Should I Learn ES6?: Classes
Why Should I Learn ES6?: Classes
Why Should I Learn ES6?: Classes
Classes
Variables
Arrow Functions
Array Methods
Destructuring
Spread Operator
Modules
Classes
ES6 introduced classes.
A class is a type of function, but instead of using the keyword function to initiate
it, we use the keyword class, and the properties are assigned inside a constructor()
method.
class Car {
constructor(name) {
this.brand = name;
}
present() {
return 'I have a ' + this.brand;
}
}
const mycar = new Car("Ford");
mycar.present();
Variables
Before ES6 there was only one way of defining your variables: with the var
keyword. If you did not define them, they would be assigned to the global object.
Unless you were in strict mode, then you would get an error if your variables were
undefined.
Now, with ES6, there are three ways of defining your variables: var, let, and const.
Arrow Functions
Arrow functions are been introduced in the ES6 version of JavaScript. It is used to
shorten the code. Here we do not use the “function” keyword and use the arrow
symbol.
hello = () => {
hello = function() { return "Hello World!";
return "Hello World!"; }
}
hello = () => "Hello World!";
const res = function (a,b) {
console.log(a+b); const sum = (a,b) => a+b;
} sum(4,6);
res(2,7);
Array Methods
map()
return value * 2;
}
</script>
When a callback function uses only the value parameter, the index and array
parameters can be omitted.
filter()
The filter() method creates a new array with array elements that pass a test.
</script>
The callback function does not use the index and array parameters, so they can be
omitted.
reduce()
</script>
The callback function does not use the index and array parameters, so they can be
omitted.
Destructuring
The two most used data structures in JavaScript are Object and Array.
Objects allow us to create a single entity that stores data items by key.
Arrays allow us to gather data items into an ordered list.
However, when we pass these to a function, we may not need all of it. The function
might only require certain elements or properties.
Destructuring Arrays
Destructuring Objects
Spread Operator
The JavaScript spread operator (...) allows us to quickly copy all or part of an
existing array or object into another array or object.
Export Import
You can import modules into a file in
You can export a function or variable two ways, based on if they are named
from any file. exports or default exports.
There are two types of exports: Named Named exports must be destructured
and Default. using curly braces. Default exports do
not.
Named Exports
In-line individually: person.js
export const name = "Jesse" Import named exports from the file
export const age = 40 person.js:
All at once at the bottom: person.js import { name, age } from "./person.js";
const name = "Jesse"
const age = 40 Example