This article records the evolution of the generation of identifiers in JS, from ES5 to ES6, ES5 and before. It only contains two declarations (varfunction ), ES6 adds some keywords that generate identifiers, such as let, const, and class.
I. ES5 Era
Var
Function
We know that JavaScript is not like other languages such as Java and Ruby. Only the keyword var is used to name variables. No matter what type of data is declared with var, the weak type does not mean that the language has no type, its types are implicitly converted at runtime (based on different operators. Other languages, such as Java, use int, float, double, and long keywords to declare numbers.
// JSvar num1 = 10; // 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;
In addition to var, the identifier in JS also has a function keyword that can generate an identifier. The identifier of a function type declaration may be a function, method, or constructor (class ).
// functionsfunction fetchData(url, param) { // ... } // methodsvar obj = { getUrl: function() { }}; // classfunction Person(name, age) {}Person.prototype = {}
Ii. ES6 Era
Var
Function
Let
Const
Class
As you can see, ES6 adds three keywords that can generate identifiers, let/const/class. Let/const is used to declare variables, and class is used to define classes.
// Define the general variable let name = 'john'; for (let I = 0; I <arr. length; I ++) {}if (boo) {let obj = {};...} // define the constant const PI = 3.1415926; const $ el = $ ('. nav'); // defines class 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 more let". let and const both have block-level scopes without variable improvements. The Declaration class also uses the class. The class keyword shares part of the function task.
The above is all the content of this article. I hope you will like it.