Introduction to JavaScript Strict mode

Source: Internet
Author: User

As we know, JavaScript is a flexible language. Its flexibility also brings a lot of pits, and of course some are design flaws. Like what

  • A variable is assigned without a declaration, and the default is a global variable, such as
    (function () {a = 3;})();console.log(window.a); // 输出3
  • An object has more than one duplicate attribute, and the last assigned property overrides the previous value. Such as
    var obj = {c: 3,c: 4}// obj 为 {c:4}

There is nothing == , typeof such as the pit, countless. After all, JavaScript's father designed the language for only one weeks, hehe.

So what are the simple ways we can avoid some pits?

Coffeescript is a good choice, but there is a cost of learning. The simpler way is to use JavaScript in strict mode.

What is the strict mode of JavaScript

In JavaScript's strict mode, there are some limitations on how JavaScript is written. If these restrictions are violated in strict mode, the code will get an error.

The purpose of setting up the strict model is mainly:

    • Reduce some of the unreasonable and less stringent JavaScript syntax, and reduce some of the bizarre behavior
    • Eliminate some of the unsafe code runs and keep your code safe
    • increase compiler efficiency and increase operating speed
    • Pave the future for new versions of JavaScript (some reserved words such as: class, Enum, export, extends, import, super cannot do variable names)

How to use

Specifies that the entire JS file executes strict mode, then the first line of the file is written

"use strict";

Specifies that a method executes strict mode, which is written on the first line of the method "use strict"; , such as

function strict(){    "use strict"; return "这是严格模式。";}

For browsers that do not support strict mode, they are ignored "use strict"; .

Restrictions in strict mode

  • Global variables must be explicitly declared
  • Prohibit usewith
  • Disable the This keyword from pointing to global objects
  • It is forbidden to traverse the call stack inside a function , such as
    function f1(){  "use strict";  f1.caller; // 报错  f1.arguments; // 报错}f1();
  • Disallow deletion of variables
  • Object cannot have a property with duplicate name
  • function cannot have a parameter with duplicate name
  • Prohibit the use of Arguments.callee
  • Prohibit assignment to arguments
  • Do not use reserved words (such as implements, interface, let, package, private, protected, public, static, yield, etc.) to make variable names

Only a few of the above are listed, more visit here.

Introduction to 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.