关于javascript:use-strict-严格模式清单

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

    'use strict'

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

    let a = 1

    v = 2

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

    'use strict'

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

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

    f()


3.限度作用域

    'use strict'
    //不容许应用 with语句

    let o = {}

    with( o ) {

      x = 1

    }

    'use strict'
    //eval 领有本身作用域

    let x = 1

    eval( 'let x = 2' )

    console.log( 'x为', x )


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

    'use strict'

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

    let x = 1

    delete x

5.显示报错

    'use strict'

    let obj = {

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

        return 3
      }
    }

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

    'use strict'

    let obj = {}

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

    obj.x = 6

    'use strict'

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

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

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

    obj.z = 6

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

function f ( x, x, y ) {

}


7.函数外部 arguments 参数 不能被批改, 无奈追踪参数变动,无奈读取callee属性

function f ( x ) {

      arguments = 6
}

    '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 )
    }

8.严格模式无奈辨认八进制示意
let  a = 012312

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理