'Nodejs class that is self describing into a file

Maybe this is basic stuff and things I want to do is really simple, but being a bit junior in Javascript I cannot find any answer to my issue.

What I want to achieve is that from code (in runtime) I want a file to be generated containing all properties and methods (including parameters and if possible return values) described.

Console logging this.__proto__ and Object.keys(this) prints somewhat what I expect. However, using the Object.keys(this.__proto__) does not return anything useable. I guess this is somehow connected to the fact that the __proto__ contains functions, which are fx. not parsable with JSON.parse().

Any ideas how to approach this?

Test code I used:

const fs = require('fs');

class Exporter {
    constructor(name) {
        this.filename = `./${name}.export`;
    }

    export(me) {
        let file = '';
        let d

        d = Object.keys(me);
        for (let i = 0; i < d.length; i++) { file += d[i] + '\n'; }
        d = Object.keys(me.__proto__);
        for (let i = 0; i < d.length; i++) { file += d[i] + '\n'; }


        fs.writeFileSync(this.filename, file);
    }
}

class Box {
    constructor(name, length, width, height) {
        this.name = name;
        this.length = length;
        this.width = width;
        this.height = height;
        this._export = new Exporter(this.name);

        this._export.export(this);
    }

    volume() {
        return this.length * this.height * this.width;
    }
}

myBox = new Box('Matts', 2, 3, 4); //creates file Matts.export

console.log(myBox.volume()); //prints '24'

File "Matts.export" written contains:

name
length
width
height
_export

So basically the Objects.keys(me.__proto__) returned a zero length array. I did expect the volume function stated. Preferably with parameters and return value. But I guess the later is hard to achieve in JavaScript.



Solution 1:[1]

Because those properties are not enumerable, you can check by using Object.getOwnPropertyDescriptors()

class Box {
    constructor(name, length, width, height) {
        this.name = name;
        this.length = length;
        this.width = width;
        this.height = height;
    }

    volume() {
        return this.length * this.height * this.width;
    }
}

let myBox = new Box("Matts",2,3,4);

console.log(Object.getOwnPropertyDescriptors(myBox.__proto__));

However,Object.getOwnPropertyDescriptors() returns an object which have the same keys as the given object. So you can get the keys from the descriptors:

class Box {
    constructor(name, length, width, height) {
        this.name = name;
        this.length = length;
        this.width = width;
        this.height = height;
    }

    volume() {
        return this.length * this.height * this.width;
    }
}

let myBox = new Box("Matts",2,3,4);

console.log(Object.keys(Object.getOwnPropertyDescriptors(myBox.__proto__)));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ricky Mo