Evolution of generation of identifiers in JS _ javascript tips-js tutorial

Source: Internet
Author: User
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.

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.