12.1 Next-Gen-Js-Summary PDF
12.1 Next-Gen-Js-Summary PDF
12.1 Next-Gen-Js-Summary PDF
let & const
Read more about let : https://developer.mozilla.org/en-US/
docs/Web/JavaScript/Reference/Statements/let
. function callMe(name) {
. console.log(name);
. }
which you could also write as:
Exports & Imports
In React projects (and actually in all modern JavaScript
projects), you split your code across multiple JavaScript
files - so-called modules. You do this, to keep each file/
module focused and manageable.
Classes
Classes are a feature which basically replace constructor
functions and prototypes. You can define blueprints for
JavaScript objects with them.
Like this:
. class Person {
. constructor () {
. this.name = 'Max';
. }
. }
.
. const person = new Person();
. console.log(person.name); // prints 'Max'
In the above example, not only the class but also a property
of that class (=> name ) is defined. They syntax you see
there, is the "old" syntax for defining properties. In modern
JavaScript projects (as the one used in this course), you
can use the following, more convenient way of defining
class properties:
. class Person {
. name = 'Max';
. }
.
. const person = new Person();
. console.log(person.name); // prints 'Max'
You can also define methods. Either like this:
. class Person {
. name = 'Max';
. printMyName () {
. console.log(this.name); // this is required to refer
to the class!
. }
. }
.
. const person = new Person();
. person.printMyName();
Or like this:
. class Person {
. name = 'Max';
. printMyName = () => {
. console.log(this.name);
. }
. }
.
. const person = new Person();
. person.printMyName();
The second approach has the same advantage as all arrow
functions have: The this keyword doesn't change its
reference.
. class Human {
. species = 'human';
. }
.
. class Person extends Human {
. name = 'Max';
. printMyName = () => {
. console.log(this.name);
. }
. }
.
. const person = new Person();
. person.printMyName();
. console.log(person.species); // prints 'human'
. const oldObject = {
. name: 'Max'
. };
. const newObject = {
. ...oldObject,
. age: 28
. };
newObject would then be
. {
. name: 'Max',
. age: 28
. }
The spread operator is extremely useful for cloning arrays
and objects. Since both are reference types (and not
primitives), copying them safely (i.e. preventing future
mutation of the copied original) can be tricky. With the
spread operator you have an easy way of creating a
(shallow!) clone of the object or array.
Destructuring
Destructuring allows you to easily access the values of
arrays or objects and assign them to variables.
. const myObj = {
. name: 'Max',
. age: 28
. }
. const {name} = myObj;
. console.log(name); // prints 'Max'
. console.log(age); // prints undefined
. console.log(myObj); // prints {name: 'Max', age: 28}
Destructuring is very useful when working with function
arguments. Consider this example: