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

javascript - Check if typescript class has settergetter - Stack Overflow

programmeradmin1浏览0评论

I have a typescript class which has the following properties:

export class apiAccount  {
    private _balance : apiMoney;
    get balance():apiMoney {
        return this._balance;
    }
    set balance(value : apiMoney) {
        this._balance = value;
    }

    private _currency : string;
    get currency():string {
        return this._currency;
    }
    set currency(value : string) {
        this._currency = value;
    }
    ...

I need to create a blank instance of this class:

let newObj = new apiAccount();

And then check if it has the setter for "currency", for example. I thought that this is exactly what getOwnPropertyDescriptor does, however apparently I was wrong:

Object.getOwnPropertyDescriptor(newObj, 'currency')
Object.getOwnPropertyDescriptor(newObj, '_currency')

These both return undefined. But chrome seems to do it! When I hover over the instance, it shows me the properties, and shows them as undefined. How can I get a list of those property names, or check if the property descriptor exists in the object?

I have a typescript class which has the following properties:

export class apiAccount  {
    private _balance : apiMoney;
    get balance():apiMoney {
        return this._balance;
    }
    set balance(value : apiMoney) {
        this._balance = value;
    }

    private _currency : string;
    get currency():string {
        return this._currency;
    }
    set currency(value : string) {
        this._currency = value;
    }
    ...

I need to create a blank instance of this class:

let newObj = new apiAccount();

And then check if it has the setter for "currency", for example. I thought that this is exactly what getOwnPropertyDescriptor does, however apparently I was wrong:

Object.getOwnPropertyDescriptor(newObj, 'currency')
Object.getOwnPropertyDescriptor(newObj, '_currency')

These both return undefined. But chrome seems to do it! When I hover over the instance, it shows me the properties, and shows them as undefined. How can I get a list of those property names, or check if the property descriptor exists in the object?

Share Improve this question asked Jun 5, 2016 at 14:52 DavidDavid 16.1k26 gold badges116 silver badges160 bronze badges 2
  • Object.keys(newObj) ? – Nitzan Tomer Commented Jun 5, 2016 at 14:58
  • Keys doesn't work for undefined properties – David Commented Jun 5, 2016 at 15:42
Add a ment  | 

1 Answer 1

Reset to default 16

The "problem" is that Object.getOwnPropertyDescriptor - as the name implies - only returns descriptors of an object's own properties. That is: only properties that are directly assigned to that object, not those that are from one of the objects in its prototype chain.

In your example, the currency property is defined on apiAccount.prototype, not on newObj. The following code snippet demonstrates this:

class apiAccount {
    private _currency : string;
    get currency():string {
        return this._currency;
    }
    set currency(value : string) {
        this._currency = value;
    }
}

let newObj = new apiAccount();
console.log(Object.getOwnPropertyDescriptor(newObj, 'currency')); // undefined
console.log(Object.getOwnPropertyDescriptor(apiAccount.prototype, 'currency')); // { get, set, ... }

If you want to find a property descriptor anywhere in an object's prototype chain, you'll need to loop with Object.getPrototypeOf:

function getPropertyDescriptor(obj: any, prop: string) : PropertyDescriptor {
    let desc;
    do {
        desc = Object.getOwnPropertyDescriptor(obj, prop);
    } while (!desc && (obj = Object.getPrototypeOf(obj)));
    return desc;
}

console.log(getPropertyDescriptor(newObj, 'currency')); // { get, set, ... }
发布评论

评论列表(0)

  1. 暂无评论