简体   繁体   English

Javascript / Typescript - 获取对象属性可见性和类型

[英]Javascript / Typescript - get object property visibility and type

My problem: 我的问题:

I need to differentiate between the private, public and getter (get X()) properties of a typescript class. 我需要区分typescript类的private,public和getter(get X())属性。

My Project: 我的项目:

I have an Angular project, that has a model design pattern. 我有一个Angular项目,它有一个模型设计模式。 Aka. 阿卡。 an user model would look like this 用户模型看起来像这样

class UserModel extends BaseModel {
    private _id: number;

    get id() { return this._id; }
    set id( _id: number ) { this._id = _id; }
}

To send these models to the backend, I just JSON.stringify() them, which if the user id is set as 13, returns an object like this 要将这些模型发送到后端,我只是JSON.stringify()它们,如果用户id设置为13,则返回一个这样的对象

{
    _id: 13
}

Now I need to modify the toJSON() function on the UserModel, so that instead of returning the private properties of the object, I will return the get X() variables only. 现在我需要修改UserModel上的toJSON()函数,这样我将返回get X()变量,而不是返回对象的私有属性。 The output should look like this. 输出应该如下所示。

{
    id: 13
}

I made this simple function, to retrieve all properties of an object, but this gives me the private properties and the get properties both. 我创建了这个简单的函数,以检索对象的所有属性,但这给了我私有属性和get属性。

abstract class BaseModel {
    public propsToObj() : {[key: string]: any} {
        let ret: any = {};

        for (var prop in this) {
            ret[prop] = this[prop];
        }

        return ret;
    }
}

and the toJSON function looks like this 并且toJSON函数看起来像这样

class UserModel extends BaseModel {
    private _id: number;

    get id() { return this._id; }
    set id( _id: number ) { this._id = _id; }

    toJSON() {
        return this.propsToObj();
    }
}

The outcome of stringify-ing the UserModel looks like this 字符串化UserModel的结果如下所示

{
    _id: 13,
    id: 13
}

In conclusion, I need to know the visibility and type (getter or regular variable?) of properties on a class, how would I achieve this? 总之,我需要知道类的属性的可见性和类型(getter或常规变量?),我将如何实现这一目标?

your propsToObj() is working wrong, it gets just all properties, you need to change it so it will get only getters, for example you can use this 你的propsToObj()工作错误,它只获取所有属性,你需要更改它以便它只获得getter,例如你可以使用它

abstract class BaseModel {
    public propsToObj() : {[key: string]: any} {
      let ret: any = {};

      for (const prop in this) {
        const descriptor = Object.getOwnPropertyDescriptor(this.constructor.prototype, prop);
        if (descriptor && typeof descriptor.get === 'function') {
            ret[prop] = this[prop];
        }
      }
        return ret;
    }
}

Object.getOwnPropertyDescriptor will get descriptor of a property and from which you can check if there is get function in descriptor, if it is then your property is getter, if not it is regular property, you can read more about descriptors here MDN(descriptors) Object.getOwnPropertyDescriptor将获取一个属性的描述符,你可以从中检查描述符中是否有get函数,如果是你的属性是getter,如果不是常规属性,你可以在这里阅读更多关于描述符的内容MDN(描述符)

The last question you asked 你提出的最后一个问题

I need to know the visibility and type of properties on a class, how would I achieve this? 我需要知道类的属性的可见性和类型,我将如何实现这一目标?

As I know you can't get the visibility of a property, as for the type if you want to know data type of a property you can use typeof for it. 据我所知,你无法获得属性的可见性,对于类型,如果你想知道属性的数据类型,你可以使用typeof

EXAMPLE in propsToObj() method: 在propsToObj()方法中的示例:

public propsToObj() : {[key: string]: any} {
      let ret: any = {};

      for (const prop in this) {
        const descriptor = Object.getOwnPropertyDescriptor(this.constructor.prototype, prop);
        if (descriptor && typeof descriptor.get === 'function') {
            ret[prop] = this[prop];
            console.log(typeof ret[prop]); // just exaple how you can know type you can save it with property too if you need it
        }
      }
        return ret;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM