Evolution of the way in which identifiers are generated in JavaScript

Source: Internet
Author: User
Tags constructor variables string

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.