Home > Article > Web Front-end > The evolution of the way identifiers are generated in JS_javascript skills
1. ES5 Era
var
function
We know that JS is not like other languages such as Java and Ruby. It only uses the keyword var to name variables. No matter what type of data, it is declared with var. Of course, weak typing does not mean that the language has no type. Its type is determined at runtime. Conversion is implicit (depending on the operator). In other languages such as Java, the keywords for declaring numbers include int, float, double, and long.
// JS var num1 = 10; // 整数 var num2 = 10.1; // 浮点数 var str = 'John'; // 字符串 var boo = false; // 布尔 var obj = {}; // 对象
// Java int num1 = 10; double num2 = 10.2; String str = "John"; Boolean boo = false;
In addition to using var to generate identifiers in JS, there is also a function keyword that can also generate identifiers. The identifier of a function type declaration may be a function, method, or constructor (class).
// functions function fetchData(url, param) { // ... } // methods var obj = { getUrl: function() { } }; // class function Person(name, age) {} Person.prototype = { }
2. ES6 Era
var
function
let
const
class
As you can see, ES6 adds three keywords let/const/class that can generate identifiers. let/const is used to declare variables, and class is used to define classes.
// 定义普通变量 let name = 'John'; for (let i = 0; i < arr.length; i++) { } if (boo) { let obj = {}; ... } // 定义常量 const PI = 3.1415926; const $el = $('.nav'); // 定义类 class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '('+this.x+', '+this.y+')'; } }
In the ES6 era, you can imagine that our coding style should be "less var and more let". Both let and const have block-level scope and no variable promotion will occur. When declaring a class, class will also be used. The class keyword shares some of the tasks of function.
The above is the entire content of this article, I hope you all like it.