This paper records the evolution of the way in which the identifier is produced in JS, from ES5 to ES6,ES5 and before it is a way, contains only two kinds of declarations (var/function), ES6 adds some keywords that produce identifiers, such as Let, Const, class.
I. The ERA of ES5
Var
function
We know that JS is not like other languages Java, Ruby, it is used to name variables only keyword var, no matter what type of data are declared with Var, of course, the weak type does not mean that the language does not have a type, its type at run time (according to different operators) will be implicitly converted. In other languages such as Java, the keywords for the light declaration numbers are int, float, double, long.
Js
var num1 = 10; Integer
var num2 = 10.1; Floating point numbers
var str = ' John '; String
var boo = false; Boolean
var obj = {}; Object
Java
int NUM1 = 10;
Double num2 = 10.2;
String str = "John";
Boolean Boo = false;
JS identifier In addition to the use of Var, there is also a function keyword can also produce identifiers. The identifier of a function type declaration may be a functional, method, or constructor (class).
Functions
function fetchdata (URL, param) {
// ...
}
Methods
var obj = {
Geturl:function () {
}
};
Class
function person (name, age) {}
Person.prototype = {
}
Ii. the era of ES6
Var
function
Let
Const
Class
As you can see, ES6 adds 3 keyword let/const/class that can produce identifiers. Let/const is used to declare variables, class is used to define classes.
Defining common variables
Let name = ' John ';
for (Let i = 0; i < arr.length; i++) {
}
if (boo) {
Let obj = {};
...
}
Defining constants
Const PI = 3.1415926;
Const $EL = $ ('. Nav ');
Defining classes
Class Point {
Constructor (x, y) {
this.x = x;
This.y = y;
}
ToString () {
Return ' (' +this.x+ ', ' +this.y+ ') ';
}
}
ES6 times, we can imagine that our code style should be "less var more let", lets and const have block-level scope, and will not occur variable elevation. The Declaration class, also uses class, the class keyword shares the function part of the task.