在功能中使用“ this”

时间:2019-02-13 07:43:59

标签: javascript ecmascript-6

在以下代码中:

var x = 10;

var arr = [1,2,3];

arr.forEach(function(arr){
  this.x = 100;
});
console.log(x); // prints '10'
console.log(this.x); //  prints 'undefined'

arr.forEach( elm =>{
  this.x = 200; 
});
console.log(x); // prints '10'
console.log(this.x); // prints '200'

有人可以解释为什么会这样吗?

先感谢

0 个答案:

没有答案