JavaScript starting point (Strict mode depth understanding) _ Basics

Source: Internet
Author: User
Strict mode (Strict mode) is a new feature of ECMASCRIPT5. ECMAScript5 can be backwards compatible with ECMASCRIPT3, but if you use strict mode, which ECMAScript5 "not recommended" ECMASCRIPT3 syntax feature will be all in the system, if it appears will throw a row. The goal of introducing strict mode is to allow developers to choose a "better" version of JavaScript, a version that handles common and notorious errors in different ways. The latest versions of all the current mainstream browsers-including IE10 and opera12---support rigorous mode. Most of the information about the strict mode can be found on page No. 223 of the ES5 specification [PDF].

How to enable strict mode
You can use strict mode on a global scale, or you can use strict mode within a function scope. If you want to enable strict mode globally, just use the code in the first line of the program:
Copy Code code as follows:

' Use strict ';

To enable strict mode within a function, simply use the first line of code in the body of the function:
Copy Code code as follows:

function Imstrict () {
' Use strict ';
// ... Other code ...
}

A statement that enables strict mode is just a normal string "use strict" with no new syntax. This means that there is no negative impact on the old browser, so it can be used boldly.

One practical application for enabling strict mode within a function is to define the entire JavaScript class library within a strictly-modal function so that it does not affect external code:
Copy Code code as follows:

(function () {
"Use strict";
Define your library strictly ...
})();

what does the strict model bring?
Before you begin to introduce special features, you need to keep in mind that one of the goals of strict mode is to allow faster debugging errors. The best way to help developers debug is to throw a corresponding error when a certain problem occurs (throw errors when certain patterns occur) instead of silently failing or behaving strangely ( This is what JavaScript does today that is not in strict mode. The code in strict mode throws more error messages, which is good because it helps developers quickly notice some problems that must be resolved immediately.

Remove with statement (eliminates with)
First, the strict mode is removed from the WITH statement. When the WITH statement appears in strict mode, it is considered an illegal JavaScript statement and throws a syntax error. So the first step in using strict mode is to make sure you're not using with.
Copy Code code as follows:

Syntax errors will result in strict mode
With (location) {
alert (HREF);
}

prevent unexpected global variables (prevents accidental globals)
The 2nd is that the variable must be declared before it is assigned a value. In a non-strict mode, assigning a value to an undeclared variable automatically generates a global variable of that name. This is one of the most common mistakes in JavaScript. In strict mode, doing so will throw an error.
Copy Code code as follows:

Throw an error in strict mode
(function () {
Someundeclaredvar = "Foo";
}());

cancels the cast of this value (eliminates this coercion)
Another important change is that when this value is null or undefined, it is not cast to the global object again. That is, this retains its original value and may cause errors in some code that relies on casting. For example:
Copy Code code as follows:

Window.color = "Red";
function Saycolor () {
This does not point to window in strict mode
alert (This.color);
}
In the following two cases, errors are thrown in strict mode
Saycolor ();
Saycolor.call (NULL);

Fundamentally, this value must be assigned, otherwise the undefined value will be preserved. This means that if you omit the new keyword when invoking the constructor, it can also cause an error:
Copy Code code as follows:

Functionperson (name) {
THIS.name = name;
}
Cause errors in strict mode
var me =person ("Nicholas");

In this code, the new keyword is missing when the person constructor is invoked, at which point the this value is undefined. Because you can't add attributes to undefined, this code throws an error. In non-strict mode, this is cast to the global object, so the Name property can be assigned the correct value as a global variable.
reject Duplicate (No Duplicates)
When you do a lot of coding, you can easily define a duplicate attribute in the object or define a duplicate parameter name for the function. In strict mode, both of these situations can cause errors to occur:
Copy Code code as follows:

Error in strict mode-duplicate parameter
function dosomething (value1, value2, value1) {
Code
}
Error in strict mode-duplicate properties
var object ={
Foo: "Bar",
Foo: "Baz"
};

Both are syntax errors, and errors are thrown before the code executes.
more Secure Eval () (Safer eval ())
The eval () has not been removed, but it has changed in strict mode. The biggest change is that the variables declared in the eval () statement and the functions are not created in the containing domain. For example:
Copy Code code as follows:

(function () {
Eval ("var x = 10;");
In non-strict mode, X is 10
In strict mode, X does not declare, throws an error
alert (x);
}());

Any variable or function created by the eval () is still in eval (). However, you can implement the delivery of values by returning a value from the eval ():
Copy Code code as follows:

(function () {
var result =eval ("var x = ten, y = 20;") x + y ");
Both strict and non rigorous mode works (get 30)
alert (result);
}());

error caused by immutable (Errors for immutables)
ECMAScript 5 also introduces the ability to modify attribute characteristics, such as setting a property to read-only or to freeze the entire object's structure (freezing an entire object ' s structure). In a non strict mode, you will silently fail when you try to modify an immutable property. You may have encountered this type of problem when using some native APIs. Strict mode will ensure that you throw an error whenever you try to modify an object or an object's properties in a way that is not allowed.
Copy Code code as follows:

var person ={};
Object.defineproperty (person, "name" {
Writable:false,
Value: "Nicholas"
});
Non-strict mode will silently fail, strict mode throws error
Person.name = "John";

In this example, the Name property is set to read-only. In a non strict mode, the assignment to name will silently fail, and in strict mode, an error will be thrown.

Note: If you are using ECMAScript attributes (the ECMAScript attribute capabilities), I strongly recommend that you open the strict mode. If you are changing the variability of the object (Mutability of objects), you will encounter a bunch of errors that will be silently removed in the non 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.