Writing high-quality JavaScript code (1-10)

Source: Internet
Author: User

1th: Understand the JS version you use

Many JS engines support the const keyword definition variable, but the ECMAScript standard does not define any semantics and behavior about the const keyword. Also, the behavior of the Const keyword is different between implementations.

In some cases, the const keyword-decorated variable cannot be updated:

1 Const PI = 3.14.592653589793; 2 PI = "Modified"; 3 // 3.14.592653589793
View Code

The other implementation simply treats the const as a synonym for Var:

Const PI = 3.14.592653589793; PI = "Modified"; PI; "Modified"

ES5 introduces another version control consideration-the strict mode (strict modes). This feature allows you to choose to disable the use of some of the more problematic or error-prone features in the JS language in the restricted JS version. Because of their backward-compatible syntax design , strict code can still be executed even in environments where strict pattern checking is not implemented.

The way to enable strict mode in a program is to add a specific string literal "use strict" at the very beginning of the program , and you can also add this directive to enable strict mode of the function at the beginning of the function body.

function f (x) {"Use strict";//...        }

If you do not test under ES5, writing code runs in a ES5 environment and is prone to error.

function f (x) {     "use strict";     var argments = [];  Error:redefinition of Argments  }

In strict mode, the argments variable is not allowed to be redefined. However, an environment that does not implement strict pattern checking will accept this code, so it will cause the program to fail in a product environment that implements ES5.

Writing high-quality JavaScript code (1-10)

Related Article

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.