Skip to main content
edited tags; edited title
Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

OOP implementation of in-memory file system [JavaScript]

Source Link
RRP
  • 121
  • 2

OOP implementation of in-memory file system [JavaScript]

I was working on this problem, where I am constructing a Object Oriented structure of file system where you can have Directories and Files. The Directories could have sub Directories and files.

I have the following layout and I am looking for feedback on improvements or if my approach is wrong.

My OOP design has three different classes Root, Directory and Files

class Root {
    constructor(name, size, writable, parent){
        this.name = name;
        this.size = size;
        this.writable = writable;
        this.parent = parent;
    }

    getName(){
        return this.name;
    };

    setName(val){
        this.name = val;
    }
}

class Directory extends Root{
    constructor(name, size, writable, parent){
        super(name, size, writable, parent);
        this.children = [];
    }

    addContent(obj){
        this.children.push(obj);
    }

    getContent(){
        return this.children;
    }
}

class File extends Root{
    constructor(name, size, writable, parent){
        super(name, size, writable, parent);
    }

    getSize(){
        return this.size;
    }
}


let root = new Root('head', 100, true);
let directory1 = new Directory('sample', 40, true, root);
let file1 = new File('test', 12, true, directory1);
let file2 = new File('test2', 14, true, directory1);
let subDirectory = new Directory('sample2', 40, true, directory1);
directory1.addContent(file1);
directory1.addContent(file2);
directory1.addContent(subDirectory);

console.log(directory1.getContent());