use strict 严格模式清单

1.变量必须得先声明后使用,否则报错

    'use strict'

    //1.变量必须得先声明后使用,否则报错

    let a = 1

    v = 2

image

2.严格模式时,禁止this关键字指向全局对象window或gobal

    'use strict'

    //2.严格模式时,禁止this关键字指向全局对象window或gobal, this为undefined

    function f () {
      console.log( 'this为', this ) // 此时this为undefined
    }

    f()

image
3.限制作用域

    'use strict'
    //不允许使用 with语句

    let o = {}

    with( o ) {

      x = 1

    }

image

    'use strict'
    //eval 拥有自身作用域

    let x = 1

    eval( 'let x = 2' )

    console.log( 'x为', x )

image
4.删除全局对象属性报错

    'use strict'

    //4.删除全局对象属性报错

    let x = 1

    delete x

image

5.显示报错

    'use strict'

    let obj = {

      get x() { // 当x只有getter时

        return 3
      }
    }

    obj.x = 6 // 对x赋值报错

image

    'use strict'

    let obj = {}

    Object.defineProperty( obj, 'x', { // x为不可写属性, 赋值会报错
      configurable: true,
      writable: false,
      value: 3
    } )

    obj.x = 6

image


    'use strict'

    // Object.freeze() 增删改 都不行
    // Object.seal() 增删 不行 改 行
    // Object.preventExtensions() 增 不行 删改行

    // 对以上方法的错误调用都会报错

    let obj = Object.preventExtensions( { x: 1, y: 2 } )

    obj.z = 6

image

6.函数参数重复命名报错

function f ( x, x, y ) {

}

image
7.函数内部 arguments 参数 不能被修改, 无法追踪参数变化,无法读取callee属性

function f ( x ) {

      arguments = 6
}

image

    'use strict'

    function f ( x ) {

      x = 666

      arguments[0]


      console.log( arguments[0] ) // arguments[0] 依旧为 1
    }

    f(1)


'use strict'


    function f ( x ) {

      console.log( arguments.callee )
    }

image

8.严格模式无法识别八进制表示
let  a = 012312

9.严格模式下 某些保留字无法使用
implements, interface, let, package, private, protected, public, static, yield。

你可能感兴趣的:(javascript严格模式)