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 = ten; Integer
var num2 = 10.1; Floating-point number
var str = ' John ';//String
var boo = false;//Boolean
var obj = {}; Object
Java
int NUM1 = ten;
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.
Define normal variable let
name = ' John ';
for (Let i = 0; i < arr.length; i++) {
}
if (boo) {let
obj = {};
...
}
Define constant
Const PI = 3.1415926;
Const $EL = $ ('. Nav ');
Define class 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.
The above mentioned is the entire content of this article, I hope you can enjoy.