Documente Academic
Documente Profesional
Documente Cultură
Ce este ES6?
ECMAScript a fost creat pentru a standardiza JavaScript și ES6 este al 6-lea versiune de
ECMAScript, a fost publicat în 2015, și este, de asemenea, cunoscut sub numele de
ECMAScript 2015.
De ce să învăț ES6?
React utilizări ES6, și ar trebui să fie familiarizați cu unele dintre noile caracteristici, cum
ar fi:
Clase
Funcții săgeată
Variabile (let, const, var)
Clase
Exemplu
class Car {
constructor(name) {
this.brand = name;
}
}
Exemplu
class Car {
constructor(name) {
this.brand = name;
}
}
mycar = new Car("Ford");
<html>
<body>
<script>
class Car {
constructor(name) {
this.brand = name;
document.write(mycar.brand);
</script>
</body>
</html>
Notă: Funcția de constructor este numit în mod automat atunci când obiectul este
inițializat.
Exemplu
class Car {
constructor(name) {
this.brand = name;
}
present() {
return 'I have a ' + this.brand;
}
}
mycar = new Car("Ford");
mycar.present();
<!DOCTYPE html>
<html>
<body>
<script>
class Car {
constructor(name) {
this.brand = name;
present() {
document.write(mycar.present());
</script>
</body>
</html>
Dupa cum se poate vedea în exemplul de mai sus, apelați metoda prin referire la numele
metodei obiectului urmat de paranteze (parametri ar merge în interiorul paranteze).
Clasa Moștenirea
O clasă creată cu o moștenire de clasă moștenește toate metodele dintr-o altă clasă:
Exemplu
Creați o clasă numită „Model“, care va moșteni metodele din clasa „auto“:
class Car {
constructor(name) {
this.brand = name;
}
present() {
return 'I have a ' + this.brand;
}
}
class Model extends Car {
constructor(name, mod) {
super(name);
this.model = mod;
}
show() {
return this.present() + ', it is a ' + this.model
}
}
mycar = new Model("Ford", "Mustang");
mycar.show();
<!DOCTYPE html>
<html>
<body>
<script>
class Car {
constructor(name) {
this.brand = name;
present() {
constructor(name, mod) {
super(name);
this.model = mod;
show() {
document.write(mycar.show());
</script>
</body>
</html>
super()Metoda se referă la clasa părinte.
Funcții săgeată
Inainte de:
hello = function() {
return "Hello World!";
}
<html>
<body>
<h1>Function</h1>
<p id="demo"></p>
<script>
hello = function() {
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>
Cu săgeată Funcția:
hello = () => {
return "Hello World!";
}
<!DOCTYPE html>
<html>
<body>
<h1>Arrow Function</h1>
<script>
hello = () => {
document.getElementById("demo").innerHTML = hello();
</script>
/body>
</html>
Ea devine mai scurt! Dacă funcția are doar o singură declarație, iar declarația returnează
o valoare, puteți elimina parantezele șireturn cuvântul cheie:
<html>
<body>
<h1>Arrow Function</h1>
<h2>Implicit Return</h2>
<p>The arrow function expects a return value, and returns the value by default, without the
<strong>return</strong> keyword.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = hello();
</script>
</body>
</html>
Arrow cu parametrii:
hello = (val) => "Hello " + val;
<html>
<body>
<h1>Arrow Function</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = hello("World");
</script>
</body>
</html>
De fapt, dacă aveți doar un singur parametru, puteți sări peste paranteze, precum:
<html>
<body>
<h1>Arrow Function</h1>
<p>As you can see in this example, you can skip the parentheses when you have only one parameter.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = hello("World");
</script>
</body>
</html>
Despre this?
Ambele exemple numesc o metodă de două ori, prima dată când se încarcă pagina, și
încă o dată când utilizatorul face clic pe un buton.
Primul exemplu utilizează o funcție normală, iar al doilea exemplu utilizează o funcție
săgeată.
rezultat arată că primul exemplu returnează două obiecte diferite (buton de ferestre și),
și al doilea exemplu returnează obiectul antet de două ori.
Exemplu
class Header {
constructor() {
this.color = "Red";
}
//Regular function:
changeColor = function() {
document.getElementById("demo").innerHTML += this;
}
}
myheader = new Header();
//The window object calls the function:
window.addEventListener("load", myheader.changeColor);
//A button object calls the function:
document.getElementById("btn").addEventListener("click", myheader.changeColor);
<html>
<body>
<h1>Regular Function</h1>
<p>The <strong>this</strong> keyword represents different objects depending on how the function was called.</p>
<p><strong>this</strong> represents:</p>
<p id="demo"></p>
<script>
class Header {
constructor() {
this.color = "Red";
changeColor = function() {
document.getElementById("demo").innerHTML += this;
window.addEventListener("load", myheader.changeColor);
document.getElementById("btn").addEventListener("click", myheader.changeColor);
</script>
</body>
Exemplu
class Header {
constructor() {
this.color = "Red";
}
//Arrow function:
changeColor = () => {
document.getElementById("demo").innerHTML += this;
}
}
myheader = new Header();
//The window object calls the function:
window.addEventListener("load", myheader.changeColor);
//A button object calls the function:
document.getElementById("btn").addEventListener("click", myheader.changeColor);
<!DOCTYPE html>
<html>
<body>
<h1>Arrow Function</h1>
<p><strong>this</strong> represents:</p>
<p id="demo"></p>
<script>
class Header {
constructor() {
this.color = "Red";
changeColor = () => {
document.getElementById("demo").innerHTML += this;
window.addEventListener("load", myheader.changeColor);
document.getElementById("btn").addEventListener("click", myheader.changeColor);
</script>
</body>
variabile
var
var x = 5.6;
Dacă utilizați în varafara unei funcții, aceasta face parte din domeniul de aplicare la nivel
mondial.
Dacă utilizați în varinteriorul unei funcții, aparține acestei funcții.
Dacă utilizați în varinteriorul unui bloc, adică o pentru buclă, variabila este încă
disponibilă în afara acelui bloc.
lăsa
let x = 5.6;
letare un bloc domeniu de aplicare.
leteste blocul de versiune scoped var, și este limitată la blocul (sau expresie) în cazul
în care este definită.
Dacă utilizați în letinteriorul unui bloc, adică o pentru buclă, variabila este disponibilă
numai în interiorul buclei.
const
const x = 5.6;
const este o variabilă că, odată ce acesta a fost creat, valoarea sa nu se poate schimba.