function foo(){
	var a=b=5;
}

console.log(a); //ReferenceError: a is not defined
console.log(b); //5

function foo(){
	"use strict;" //加上严格模式
	var a=b=5;
}

console.log(a); //ReferenceError: a is not defined
console.log(b); //ReferenceError: b is not defined

解析
因为 var a = b = 5 等价于

var a;
b = 5;
a = b;

在非严格模式中,未声明变量默认为全局变量,可以在全局环境中通过this访问。
严格模式取消了默认this全局变量,因此在函数foo外访问会报错。

相关文章:

  • 2021-11-22
  • 2021-12-01
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2021-12-22
  • 2022-12-23
猜你喜欢
  • 2022-01-03
  • 2021-09-05
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案