Strict Javascript mode and javascript Mode

Source: Internet
Author: User

Strict Javascript mode and javascript Mode
Introduction

Strict mode is a way to introduce better error checking into the code. When using the strict mode, you cannot use variables that are implicitly declared, assign values to read-only properties, or add attributes to non-extensible objects.

Statement strict Mode

You can declare the strict mode by adding "use strict"; at the beginning of a file, program, or function. Such statements are called the preface to commands ". The scope of the strict mode Declaration depends on its context. If the strict mode is declared in the Global Context (outside the scope of the function), all code in the program is in the strict mode. If the strict mode is declared in the function, all code in the function is in the strict mode. For example, in the following example, all code is in strict mode, and the variable declaration outside the function may cause a syntax error "undefined variable in strict mode ".

 

[Javascript]View plaincopyprint?
  1. "Use strict ";
  2. Function testFunction (){
  3. Var testvar = 4;
  4. Return testvar;
  5. }
  6. // An error is reported here.
  7. Testvar = 5;

 

 

In the following example, only the code in testFunction is in strict mode. The variable declaration outside the function does not cause syntax errors, but the internal declaration of the function may cause syntax errors.

 

[Javascript]View plaincopyprint?
  1. Function testFunction (){
  2. "Use strict ";
  3. // An error is reported here.
  4. Testvar = 4;
  5. Return testvar;
  6. }
  7. Testvar = 5;


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.