Home

浏览器与node,this,的不同

区别

浏览器

  • Chrome + Firefox
function local() {
	this.a = 7;
	var a = 2;
	this.a = 7;
	console.log(a);
	return this;
}
var local1 = local();
console.log(a);
local1 == this ? console.log(a) : console.log(0);

输出结果:

2
7
7

node

v6.9.2

对于同样的代码

function local() {
	this.a = 7;
	var a = 2;
	this.a = 7;
	console.log(a);
	return this;
}
var local1 = local();
console.log(a);
local1 == this ? console.log(a) : console.log(0);

输出结果:

2
7
0
[Finished in 0.7s]
console.log(this);//{}
console.log(global);//才是全局变量。在node中!