javascript - ES6 箭头函数可以有自己的词法 this 吗?

标签 javascript ecmascript-6 this

function Person(){
  this.age = 0;

  setInterval(() => {
    this.age++; // this refers to the person object
  }, 1000);
}

但是如果我想访问 setInterval 函数的“this”而不是父对象怎么办?我知道箭头函数没有自己的词法 this,但是除了编写非箭头函数之外,还有其他方法可以绕过它吗?

最佳答案

Is there a way to circumvent this, other than writing a non-arrow function?

没有。

传递给箭头函数的 this 值会被它忽略,并且不能以任何形式使用。

关于javascript - ES6 箭头函数可以有自己的词法 this 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48988017/

相关文章:

javascript - 如何使用 ECMAScript 6 Unicode 代码点转义?

c# - 正确使用 "this"关键字吗?

javascript - 将 "this"引用到自定义变量

javascript - 在javascript中将数组本身合并到另一个具有相同键的数组中

javascript - 如何在 localStorage API HTML5 中保存函数

JavaScript - 使用 ES* 模块优化下载时间

javascript - 从 console.log 自定义打印?

javascript - 事件监听器未在循环中工作

javascript - 有没有办法在 JavaScript 加载之前不加载 html?

java - this 关键字和继承 - Java