小编Pra*_*nth的帖子

ES6 函数在解构对象时无权访问“this”

我在 ES6 学习材料中尝试了以下问题

const circle = {
  radius: 10,
  color: 'orange',
  getArea: function() {
    return Math.PI * this.radius * this.radius;
  },
  getCircumference: function() {
    return 2 * Math.PI * this.radius;
  }
};

let {radius, getArea, getCircumference} = circle;
console.log(getArea())
Run Code Online (Sandbox Code Playgroud)

起初我以为打印的结果是,314.1592653589793但结果打印的结果是NaN. 这意味着该getArea()函数无权访问this. 为什么this在解构 Object 时该函数无权访问?

javascript ecmascript-6 object-destructuring

2
推荐指数
1
解决办法
84
查看次数