Evolution _javascript techniques of generating identifiers in JS

Source: Internet
Author: User

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.

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.