Summary of JS strict mode knowledge points and js knowledge points

Source: Internet
Author: User

Summary of JS strict mode knowledge points and js knowledge points

The so-called strict mode is actually a string that does not assign a value to any variable "use strict"

If this prompt is displayed in the global scope, the entire script will adopt the strict mode. You can also enable the strict mode only in the function.

1. No global variables in strict Mode

A = "test"

An error is reported in strict mode, but not in strict mode.

2. delete variables

Var has three types of declarations.

  • Global variables declared by var
  • Local variables declared by var
  • Global variables declared in eval ()

The first and second cases cannot be deleted using delete.

First, although the Global Object declared in the first case is a windows attribute, the resumable = false attribute of this attribute cannot be deleted.

In the second case, the local variable is even worse. No one knows the attached object and how to delete it.

In the third case, eval () will be mentioned later.

Return to the question. That is to say, variables can be deleted in non-strict mode, but false is returned if a failure occurs.

If you delete a variable in strict mode, an error is returned.

3. Object

An error is reported when the operation object is in the following circumstances.

  1. An error is returned when you assign a value to the read-only attribute.
  2. If you use delete For unconfigurable properties, an error is returned.
  3. An error is reported when you add attributes to an object that cannot be expanded.
  4. When using the object literal, the attribute name must be unique. For example
var person={  name:"1",  name:"2"}

In non-strict mode, the second value is selected by default, and an error is returned in strict mode.

4. Functions

Strict mode requires that the function parameter name must be unique

function(n,n){  // todo}

In non-strict mode, this function declaration does not report an error. Only the second parameter can be accessed through the parameter name. The first parameter must be accessed through arguments.

Arguments are different in both modes.

In non-strict mode, modifying the name parameter value is reflected in the arguments object. In strict mode, the two values are independent.

Arguments. callee (reference function itself) and arguments. caller (reference to call function) are eliminated ).

In strict mode, function names cannot use JavaScript reserved words.

** In strict mode, you can declare a function only at the top level of the script and within the function. Declaring a function in the if statement causes a syntax error. **

If (true) {function () {// error in strict mode }}

5. this

If you use the apply (), call () function in non-strict mode, the null or undefined value is converted to a global object. In strict mode, this of a function is always the specified value, regardless of the specified value.

Var a = "1"; function test () {console. log (this. a)} test. call (null) Outputs 1 in non-strict mode. In strict mode, this refers to null. If null does not have the attribute, an error is returned.

 

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.