What is ES6?
ES6 stands for ECMAScript 6. ECMAScript was created to standardize JavaScript, and ES6 is the 6th version of ECMAScript, it was
published in 2015, and is also known as ECMAScript 2015.
Why Should I Learn ES6?
React uses ES6, and you should be familiar with some of the new features like:
Classes
Arrow Functions
Variables (let, const, var)
Classes
A class definition can include constructors and methods. The classes contain constructors that allocate the memory to the objects of a
class. Classes contain methods that are responsible for performing the actions to the objects.
ExampleA simple class constructor:
class Car { | |
constructor(name) { | |
this.brand = name; | |
} | |
} |
Now you can create objects using the Car class:
Create an object called "ford" based on the Car class. The constructor function is called automatically when the object is initialized.
<!DOCTYPE html> | |
<html> | |
<body> | |
<script> | |
class Car { | |
constructor(name) { | |
this.brand = name; | |
} | |
} | |
ford = new Car("Ford"); | |
acura= new Car("Acura"); | |
audi= new Car("Audi"); | |
document.write(ford.brand); | |
document.write(acura.brand); | |
document.write(audi.brand); | |
</script> | |
</body> | |
</html> |
Method in Classes
You can add your own methods in a class. Create a method named "getBrand":
<!DOCTYPE html> | |
<html> | |
<body> | |
<script> | |
class Car { | |
constructor(name) { | |
this.brand = name; | |
} | |
getBrand() { | |
return 'I have a ' + this.brand; | |
} | |
} | |
ford = new Car("Ford"); | |
document.write(ford.getBrand()); | |
</script> | |
</body> | |
</html> |
(parameters would go inside the parentheses).
Class Inheritance
<!DOCTYPE html> | |
<html> | |
<body> | |
<script> | |
class Animal { | |
constructor(name, speed) { | |
this.speed = speed; | |
this.name = name; | |
document.write("Inside Animal class <br/>"); | |
} | |
run() { | |
return 'My name is ' + this.name + '. I am running with speed ' + this.speed; | |
} | |
} | |
dog = new Animal("Dog", 5); | |
document.write(dog.name + "<br/>") | |
document.write(dog.run() + "<br/>"); | |
class Rabbit { | |
constructor(name, speed) { | |
this.speed = speed; | |
this.name = name; | |
document.write("Inside Rabbit class <br/>"); | |
} | |
run() { | |
return 'My name is ' + this.name + '. I am running with speed ' + this.speed; | |
} | |
hide() { | |
return this.name + " hides!"; | |
} | |
} | |
florida = new Rabbit("Florida White rabbit", 5); | |
document.write(florida.run() + "<br/>"); | |
document.write(florida.hide()); | |
</script> | |
</body> | |
</html> |
To create a class inheritance, use the extends keyword.
A class created with a class inheritance inherits all the methods from another class.
Create a class named "Rabbit" which will inherit the methods from the "Animal" class.
<!DOCTYPE html> | |
<html> | |
<body> | |
<script> | |
class Animal { | |
constructor(name, speed) { | |
this.speed = speed; | |
this.name = name; | |
document.write("Inside Animal class <br/>"); | |
} | |
run() { | |
return 'My name is ' + this.name + '. I am running with speed ' + this.speed; | |
} | |
} | |
class Rabbit extends Animal { | |
// Overriding the parent class functionality | |
run() { | |
return 'My name is ' + this.name + '. I am jummping with speed ' + this.speed; | |
} | |
hide() { | |
return this.name + " hides!"; | |
} | |
} | |
florida = new Rabbit("Florida White rabbit", 5); | |
document.write(florida.run() + "<br/>"); | |
document.write(florida.hide() + "<br/>"); | |
document.write("Rabbit name " + florida.name); | |
</script> | |
</body> | |
</html> |
Calling parent class from child class
<!DOCTYPE html> | |
<html> | |
<body> | |
<script> | |
class Animal { | |
constructor(name, speed) { | |
this.name = name; | |
this.speed = speed; | |
document.write("Inside Animal class <br/>"); | |
} | |
run() { | |
return 'My name is ' + this.name + '. I am running with speed ' + this.speed; | |
} | |
} | |
class Rabbit extends Animal { | |
constructor(name, speed) { | |
// Calling the parent class constructor with name, speed parameters | |
super(name, speed); | |
document.write("Inside Rabbit class <br/>"); | |
} | |
hide() { | |
return this.name + " hides!"; | |
} | |
} | |
florida = new Rabbit("Florida White rabbit", 5); | |
document.write(florida.run() + "<br/>"); | |
document.write(florida.hide() + "<br/>"); | |
document.write("Rabbit name " + florida.name); | |
</script> | |
</body> | |
</html> |
Arrow Functions
Arrow functions were introduced in ES6. Arrow functions allow us to write shorter function syntax:
Before:
<!DOCTYPE html> | |
<html> | |
<body> | |
<h1>Function</h1> | |
<p>This demonstrates a regular function, NOT an arrow function.</p> | |
<p id="demo"></p> | |
<script> | |
hello = function() { | |
return "Hello World!"; | |
} | |
document.write(hello()); | |
</script> | |
</body> | |
</html> |
With Arrow Function:
<!DOCTYPE html> | |
<html> | |
<body> | |
<h1>Arrow Function</h1> | |
<p>A demonstration of a simple arrow function.</p> | |
<p id="demo"></p> | |
<script> | |
hello = () => { | |
return "Hello World!"; | |
} | |
document.write(hello()); | |
</script> | |
</body> | |
</html> |
It gets shorter. If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword:
Arrow Functions Return Value by Default: This works only if the function has only one statement.
hello = () => "Hello World!";
Arrow Function With Parameters: If you have parameters, you pass them inside the parentheses.
hello = (val) => "Hello " + val;
Arrow Function Without Parentheses: In fact, if you have only one parameter, you can skip the parentheses as well.
hello = val => "Hello " + val;
Variables
Before ES6, there was only one way of defining your variables with the var keyword. Now, with ES6, there are three ways of defining your variables: var, let, and const.
var
var x = 5.6;
let
let x = 5.6;
const
const x = 5.6;
const is a variable that once it has been created, its value can never change.
const has a block scope.
No comments:
Post a Comment