JavaScript Strict mode

Source: Internet
Author: User

Strict mode

ECMAscript 5 Adds a strict mode of operation, which means that JavaScript runs under more stringent conditions. Mainstream browsers, including IE 10, have supported him, and many big projects have embraced him, defining the purpose of strict patterns

1 ... eliminate some of the irrational, less rigorous, and less bizarre behavior of JavaScript syntax

2 ... eliminate some of the unsafe code from running and keep your code safe

3. Improve compilation efficiency, increase running speed

4. To pave the new version of JavaScript

Enable JavaScript Strict mode

Just add the following comment string to the JavaScript code header

<script>    "use strict";     // write JavaScript code below    // Note that you want to enable strict mode,   "use strict  "    console.log ("This is strict mode"); </script>
Global mode

Put "use strict" in the first line, the entire script will run in strict mode, if not in the first row, will run in normal mode, the browser will be a string of ordinary string to ignore it

If the code files of different schemas are merged into one file, this requires special attention, strictly speaking, as long as the preceding code is not the statement that produces the actual result of the operation, "use strict" may not be in the first row

Local mode

  Put "use strict" in the first row within the function, the entire function will run in strict mode.

function Strict () {  "use strict"  return "This is strict mode";    } function notstrict () {  return "This is normal mode";  }
Module mode

Because the global schema is not conducive to file merging, it is better to borrow the local schema method to place the entire script file in an anonymous function that executes immediately

Example: If you define a module or library, you can design it in a way that is self-executing by an anonymous function:

(function() {  "use strict"  // write JavaScript code    here });

JavaScript Strict mode

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.