历史

ES5 严格模式

写法

// 写在所有代码的最上方
'use strict'; // 严格模式 字符串

// 写在函数内部第一行(推荐)
function test() {
	'use strict';
}

🌈 with 可以改变作用域

var a = 1;
var obj = {
	a: 2
}
function test() {
	var a = 3;
  with(test) {
  	console.log('test->>', a);
  }
  with(window) {
  	console.log('window->>', a);
  }
  with(obj) {
  	console.log('obj->>', a);
  }
}
test();

Untitled

严格模式下报错

'use strict';
var a = 1;
var obj = {
	a: 2
}
function test() {
	var a = 3;
  with(test) {
  	console.log('test->>', a);
  }
}
test();

Untitled

callee / caller 严格模式下报错

'use strict';
function test() {
	console.log(arguments.callee)
}
test();

Untitled

'use strict';
function test() {
	test2();
}
function test2() {
	console.log(test2.caller)
}
test();

Untitled

严格模式下,变量需要显示声明

'use strict';
function test() {
	var a = b = 1;
}
test();