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.
- An error is returned when you assign a value to the read-only attribute.
- If you use delete For unconfigurable properties, an error is returned.
- An error is reported when you add attributes to an object that cannot be expanded.
- 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.