Detailed javascript strict mode (with the distinction between strict and non-strict modes)

Keywords: Javascript Attribute

Advantages and disadvantages of strict mode

Advantage:

  1. Improving code parsing and running speed
  2. Disable some unreasonable syntax to reduce code weird behavior

shortcoming

  1. Some code will fail in strict mode, especially when introducing common and third-party modules
  2. Some strict mode features are supported differently in different browsers and compatibility issues need to be noted

The difference between strict mode and non-strict mode

1. Disable with syntax, use will error

Because the scope can be very complex when parsing with syntax, it can seriously affect the speed of code parsing and running.

function usualMode() {
    with({a: 1}) {
        console.log(a)
    }
}
usalMode() // Normal Output 1

function strictMode() {
    'use strict'
    with({a: 1}) {
        console.log(a)
    }
}
strictMode() // Error will be reported

2. Prevent deletion of variables and functions

function usualMode() {
    function fn() {} 
    var a = 1
    delete a // No error, but the variable a could not actually be deleted
    delete fn // Same delete a
}
usalMode() // Normal execution

function strictMode() {
    'use strict'
    function fn() {} 
    var a = 1
    delete a
}
strictMode() // Error will be reported

3. Property Descriptor correlation

Methods that can change attribute descriptors are Object.defineProperty, Object.defineProperties, Reflect.defineProperty, Reflect.defineProperties, Object.freeze, Object.seal; Obtaining an attribute descriptor can use Object.getOwnPropertyDescriptor, Object.getOwnPropertyDecriptors,ES6 also has Reflect.getOwnPropertyDescriptor, Reflect.getOwnPropertyDescriptors

3.1 Error occurs when deleting the property configurable = false

'use strict'
var obj = {}
Object.defineProperty(obj, 'a', {
    configurable: false,
    value: 1
})
delete obj.a // Strict mode will error; non-strict mode will return false

3.2 Error in assigning a property to writable = false

'use strict'
var obj = {}
Object.defineProperty(obj, 'a', {
    writable: false,
    value: 1
})
obj.a = 2 // Strict mode will fail; non-strict mode will not fail, but will not work, obj.a still equals 1

4. Adding attributes to object s that do not allow expansion will cause errors

'use strict'
var obj = {a: 1}
Object.preventExtensions(obj)
obj.b = 2 // Errors will be reported in strict mode; non-strict mode will not report errors, but will not take effect if'b'in obj is false

Methods that can set an object to be non-extensible are Object.freeze, Object.seal, Object.preventExtensions;ES6 also has Reflect.freeze, Reflect.seal, Reflect.preventExtensions; determine whether an object allows extensions to be used with Object.isExtensible;ES6 also has Reflect.isExtensible

5. Assigning an undeclared variable will result in an error

'use strict'
a = 1 // Errors will be reported in strict mode, and non-strict mode a variable will be promoted to global scope

6. Property rename error when defining object

'use strict'
var obj = {a: 1, a: 2}// Strict mode will error; a after non-strict mode will overwrite the previous a, obj.a = 2

7. Errors will be reported when formal parameters are repeated

'use strict'
function fn(a, a) {
    console.log(a, arguments)
}
fn(1,2) // Strict mode will fail; non-strict mode will not fail, a=2, both parameters in arguments

8.eval correlation

8.1 Eval has independent scope

'use strict'
eval('var a = 1')
console.log(typeof a) // undefined in strict mode; number in non-strict mode

8.2eval cannot be used as variable or function names, similar to keywords

'use strict'
var eval = 1 // Error will be reported in strict mode; non-strict mode will declare a variable eval with a value of 1

// Error will be reported in strict mode; non-strict mode will declare a corresponding eval function
function eval() {
    // some code
}

9.arguments correlation

9.1 arguments is a copy of the parameter (similar to a shallow copy)

'use strict'
function fn(a, obj){
    arguments[0] = 2
    arguments[1].b = 2
    console.log(a) // Strict mode 1; non-strict mode 2
    console.log(obj.b) // 2, because object in js is address delivery
}
fn(1, {b: 1})

9.2arguments cannot be used as variable or function names, similar to keywords

'use strict'
var arguments = 1 // Errors will be reported in strict mode; non-strict mode will declare a variable arguments with a value of 1

// Errors will be reported in strict mode; non-strict mode will declare a corresponding arguments function
function arguments() {
    // some code
}

10. Disable caller and callee

'use strict'
function fn() {
    console.log(arguments.callee.caller, fn.caller) // Error in strict mode; non-strict mode points to fn2
    console.log(arguments.callee) // Strict mode error; non-strict mode points to fn
}
function fn2() {
    fn()
}
fn2()

Posted by shaunrigby on Mon, 11 Nov 2019 14:12:24 -0800