js严格模式特点

34 阅读1分钟

开启严格模式

'use strict' // 全局开启

function fn() {
    'use strict' // 局部开启
    
}

特点

全局变量必须先声明

'use strict'
n = 100 // ❌

禁止用with

const user= {name: 'jack', age: 11}
with(user) {
    console.log(name, age)
}

创建eval作用域

'use strict'
var a = 10
eval('var a = 20; console.log(a)') // 20
console.log(a) // 10

禁止this指向window

'use strict'
functoin a() {
    console.log(this) // undefind
}
a()
functoin a() {
    console.log(this) // window
}
a()