this有意思的小题

偶然发现的一道小题,原题:

1
2
3
4
5
6
7
8
9
function a(xx){
this.x = xx;
return this
};
var x = a(5);
var y = a(6);

console.log(x.x) // undefined
console.log(y.x) // 6

刚开始没明白为啥是这个结果,最后才发现这个问题有点坑

1
2
3
4
5
6
7
8
9
10
function a(xx){
this.x = xx;
console.log(this)
return this
};
var x = a(5);
var y = a(6);

console.log(x) // 6
console.log(y) // window

y=a(6)未执行的时候,x是window

y=a(6)执行的时候,会将window.x重新赋值,所以打印x.x=undefined