ES6类没有列出getset属性

问题描述 投票:0回答:1

我有以下代码

const obj1 = {
  prop1: 'a',
  get prop2() {
    return this.prop1;
  }
};
console.log("obj1")
console.log(Object.getOwnPropertyDescriptors(obj1))

class c1 {
  constructor() {
    this.prop1 = 'b';
  }
  get prop2() {
    return this.prop1;
  }
}
let obj2 = new c1()
console.log("obj2")
console.log(Object.getOwnPropertyDescriptors(obj2));

我以为两个对象都应该把 "prop2 "列为属性,但只有obj1是这样做的,我的代码是const obj1 = { prop1: 'a', get prop2({ return this.prop1; }。

javascript properties es6-class
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.