This paper records the evolution of the method of generating identifiers in JS, from ES5 to ES6,ES5 and before it is a way, which contains only two declarations (var/function), and ES6 adds some keywords that generate an identifier, such as Let, Const, class.
First, ES5 era
- Var
- function
We know that JS is not like other languages Java, Ruby, and so on, it is used to name variables of the 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 number are int, float, double, long.
Jsvar num1 = ten; Integer var num2 = 10.1; Floating-point var str = ' John ';//string var boo = false; boolean var obj = {}; Object
Javaint num1 = 10;double num2 = 10.2; String str = "John"; Boolean Boo = false;
JS identifier In addition to the use of VAR generated, 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).
Functionsfunction fetchdata (URL, param) { //... } Methodsvar obj = { geturl:function () { }};//classfunction 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 generate identifiers. Let/const is used to declare variables, class is used to define classes.
Define the normal variable let name = ' John ', for (let i = 0; i < arr.length; i++) {}if (boo) {let obj = {}; ...} Defines a const PI = 3.1415926;const $el = $ ('. Nav ');//Defines a class of type ' point ' {constructor (x, y) {this.x = x; This.y = y;} ToString () { return ' (' +this.x+ ', ' +this.y+ ') '; }}
In the ES6 era, we can imagine that our code style should be " less var and morelet", with both allow and const have block-level scopes, and no variable promotion occurs. The Declaration class, too, uses class, and the class keyword shares some of the function's tasks.
Iii. the era of ES7
...
Related:
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/let
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/const
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/class
Evolution of how identifiers are generated in JavaScript