Knowledge about ecmascript6

Source: Internet
Author: User
  • Ecmascript is the latest standard of the current JavaScript language specification, which is generally referred to as es6. However, because the standard specification was released in June 2015, it is also called ecmascript 2015.
  • Let variable Declaration

Es2015 adds a variable declaration method:let, Similarvar, ThroughletThe declared variables are onlyletThe code block in which the command resides is valid.

Block-level scopes. Block-level codes automatically form a separate scope.

Why block-level scope?

  1. The inner variable may overwrite the outer variable.
  2. The cyclic variables used for counting are leaked as global variables.

 

UseletNOTE: If there is no variable escalation, you must declare the variable before using it. Otherwise, an error is returned. Duplicate declarations of variables with the same name are not allowed within the same scope; otherwise, an error is reported. Block-level scope. The outer code block is not affected by the inner code block. Variables in the inner scope cannot be read from the outer scope. The inner scope can define variables with the same name as the outer scope.

Use Cases: Let variables are generally used to declare the block.

  • Const variable Declaration 

Const is also used to declare variables, but declared as constants. Once declaredValueIt cannot be changed. The constant value cannot be changed; otherwise, an error is returned.constIt is also a block-level scope. It is only valid within the block-level scope where the declaration is located. We do not recommend that you use the const variable in the block to promote it. You cannot declare it again.

Const application scenarios:

All variables that do not want the user to change are passedconstDeclared as a constantjavascript const fs = require(‘fs‘); const os = require(‘os‘);

PassconstThe declared constant cannot be changed by the constant address. If the constant is an object, it can still be modified.

javascript ‘use strict‘; const obj = { foo: ‘bar‘ }; console.log(obj.foo); // => bar obj.foo = ‘baz‘; console.log(obj.foo); // => baz

 

Knowledge about ecmascript6

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.