js严格模式
灵感胜于汗水 Lv5

严格模式

使用严格模式

1
"use strict"

变量

1
2
a = 10
console.log(a)//严格模式报错(必须使用var等声明变量)

静默失败 => 报错

1
2
3
4
5
6
7
8
9
var b = 2
console.log(Object.getOwnPropertyDescriptor(window, 'b'))
/*configurable: false //var声明的变量默认不可删除
enumerable: true
value: 2
writable: true*/
delete b//未删除,非严格模式下不报错,程序继续进行(静默失败)
//严格模式下报错
console.log(b)//2

函数

参数唯一

1
2
3
4
function test(a,a){
console.log(a)//undefined(第二个a),严格模式报错
}
test(10)

实参和形参的映射关系不存在

1
2
3
4
5
6
7
8
9
10
11
12
13
function test1(a) {
a = 20
console.log(arguments[0])//非严格模式:20,严格模式:10
}

test1(10)

function test2(a = 20) {//参数设置初始后自动开启严格模式
a = 30
console.log(arguments[0])//10
}

test2(10)

函数的this默认指向undefined

1
2
3
4
function test(){
console.log(this)//undefined
}
test()

不能使用arguments.callee和caller

1
2
3
4
5
6
7
8
function test(){
console.log(arguments.callee === test)//true,严格模式无法使用
console.log(test.caller === out)//true,严格模式无法使用
}
function out(){
test()
}
out()

不能使用eval()和with()

1
2
3
4
5
6
7
8
9
10
11
12
eval('var a=2')
console.log(a)//2,严格模式无法使用

const obj={
a:3
}
function test(){
with (obj) {//改变this指向
console.log(a)//3,严格模式无法使用
}
}
test()

eval和arguments不能作为标识符

1
let eval='111'//严格模式报错
  • 本文标题:js严格模式
  • 本文作者:灵感胜于汗水
  • 创建时间:2022-03-09 13:51:37
  • 本文链接:https://cjhsyc.github.io/2022/03/09/js严格模式/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!