最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot get property descriptor of a class property - Stack Overflow

programmeradmin5浏览0评论

I want to inspect an instance of a JavaScript class and access its getter. In ES5, I can write this code to retrieve the getter of an object:

var obj = {
  get foo () {}
};

const foo = Object.getOwnPropertyDescriptor (obj, 'foo').get;
// returns a function

However, when I try this on a class instance, my code fails:

class Foo {
  get foo () {}
}

var obj = new Foo ();
const foo = Object.getOwnPropertyDescriptor (obj, 'foo').get;
// error: Cannot read property 'get' of undefined

Object.getOwnPropertyDescriptor does not seem to work: it returns undefined for the foo property.

I am using Babel 6.4.5 to transpile my code from ES2015 to ES5.

Is Object.getOwnPropertyDescriptor supposed to work on classes too? Or is this a side effect of using Babel?


EDIT I've finally switched to Object.getOwnPropertyDescriptor as suggested by Bergi. I describe the solution in detail in a blog post (Enumerating methods on a JavaScript class instance).

I want to inspect an instance of a JavaScript class and access its getter. In ES5, I can write this code to retrieve the getter of an object:

var obj = {
  get foo () {}
};

const foo = Object.getOwnPropertyDescriptor (obj, 'foo').get;
// returns a function

However, when I try this on a class instance, my code fails:

class Foo {
  get foo () {}
}

var obj = new Foo ();
const foo = Object.getOwnPropertyDescriptor (obj, 'foo').get;
// error: Cannot read property 'get' of undefined

Object.getOwnPropertyDescriptor does not seem to work: it returns undefined for the foo property.

I am using Babel 6.4.5 to transpile my code from ES2015 to ES5.

Is Object.getOwnPropertyDescriptor supposed to work on classes too? Or is this a side effect of using Babel?


EDIT I've finally switched to Object.getOwnPropertyDescriptor as suggested by Bergi. I describe the solution in detail in a blog post (Enumerating methods on a JavaScript class instance).

Share Improve this question edited Jan 26, 2016 at 19:57 Pierre Arnaud asked Jan 26, 2016 at 15:30 Pierre ArnaudPierre Arnaud 10.5k13 gold badges82 silver badges112 bronze badges 3
  • The examples are pletely different. Your first example creates an object that has a foo getter, the second one creates an object that has a prototype which has a foo getter. – a better oliver Commented Jan 26, 2016 at 15:40
  • Ah, yes. I have ovelooked that! I've still to get my mind around how ES6 classes get mapped to ES5. – Pierre Arnaud Commented Jan 26, 2016 at 15:52
  • "I've still to get my mind around how ES6 classes get mapped to ES5" class Foo { constructor() { /*constructor*/ } method() {} }, is (almost) the same as function Foo() { /*constructor*/ } Foo.prototype.method = function() {};, except that methods are not enumerable. – Felix Kling Commented Jan 26, 2016 at 16:03
Add a ment  | 

1 Answer 1

Reset to default 26

It does work with classes, but the instance you tried it on has no own property. Use

Object.getOwnPropertyDescriptor(Object.getPrototypeOf(obj), 'foo')
Object.getOwnPropertyDescriptor(Foo.prototype, 'foo')
发布评论

评论列表(0)

  1. 暂无评论