Using JSON.stringify on custom class

前端 未结 5 2001
执笔经年
执笔经年 2020-12-06 12:51

I\'m trying to store an object in redis, which is an instance of a class, and thus has functions, here\'s an example:

function myClass(){
    this._attr = \"         


        
5条回答
  •  忘掉有多难
    2020-12-06 13:22

    it looks like you attempt to stringify a closed function. you can use ()=>{} to solve the scope problem.

    function myClass(){
        this._attr = "foo";
        this._accessCounts = 0;
        this.getAttr = ()=>{
            this._accessCounts++;
            return this._attr;
        }
        this.getCount = ()=>{
            return this._accessCounts;
        }
    }
    

提交回复
热议问题