How to write a object oriented Node.js model

30,722

Solution 1

If I were to design an object like this, then I would have done like this

function Cat(age, name) {       // Accept name and age in the constructor
    this.name = name || null;
    this.age  = age  || null;
}

Cat.prototype.getAge = function() {
    return this.age;
}

Cat.prototype.setAge = function(age) {
    this.age = age;
}

Cat.prototype.getName = function() {
    return this.name;
}

Cat.prototype.setName = function(name) {
    this.name = name;
}

Cat.prototype.equals = function(otherCat) {
    return otherCat.getName() == this.getName()
        && otherCat.getAge() == this.getAge();
}

Cat.prototype.fill = function(newFields) {
    for (var field in newFields) {
        if (this.hasOwnProperty(field) && newFields.hasOwnProperty(field)) {
            if (this[field] !== 'undefined') {
                this[field] = newFields[field];
            }
        }
    }
};

module.exports = Cat;     // Export the Cat function as it is

And then it can be used like this

var Cat = require("./Cat.js");

var cat1 = new Cat(12, 'Tom');
cat1.setAge(100);
console.log(cat1.getAge());                 // 100

var cat2 = new Cat(100, 'Jerry');
console.log(cat1.equals(cat2));             // false

var sameAsCat1 = new Cat(100, 'Tom');
console.log(cat1.equals(sameAsCat1));       // true

var sameAsCat2 = new Cat();
console.log(cat2.equals(sameAsCat2));       // false

sameAsCat2.fill({name: "Jerry", age: 100});
console.log(cat2.equals(sameAsCat2));       // true

Solution 2

I would use a class :

class Cat {
    fields = {
        age: null,
        name: null
    };

    fill(newFields) {
        for(var field in this.fields) {
            if(this.fields[field] !== 'undefined') {
                this.fields[field] = newFields[field];
            }
        }
    }

    getAge() {
        return this.fields.age;
    }

    setAge(newAge) {
        this.fields.age = newAge;
    }
}

exports.Cat = Cat;
Share:
30,722
letter Q
Author by

letter Q

Updated on July 09, 2022

Comments

  • letter Q
    letter Q almost 2 years

    I am having a lot of trouble writing an object oriented Cat class in Node.js. How can I write a Cat.js class and use it in the following way:

    // following 10 lines of code is in another file "app.js" that is outside 
    // the folder "model"
    var Cat = require('./model/Cat.js');
    
    var cat1 = new Cat(12, 'Tom');
    cat1.setAge(100);
    console.log(cat1.getAge()); // prints out 100 to console
    
    var cat2 = new Cat(100, 'Jerry');
    console.log(cat1.equals(cat2)); // prints out false
    
    var sameAsCat1 = new Cat(100, 'Tom');
    console.log(cat1.equals(sameAsCat1)); // prints out True
    

    How would you fix the following Cat.js class I have written:

     var Cat = function() {
        this.fields = {
            age: null,
            name: null
        };
    
        this.fill = function (newFields) {
            for(var field in this.fields) {
                if(this.fields[field] !== 'undefined') {
                    this.fields[field] = newFields[field];
                }
            }
        };
    
        this.getAge = function() {
            return this.fields['age'];
        };
    
        this.getName = function() {
            return this.fields['name'];
        };
    
        this.setAge = function(newAge) {
            this.fields['age'] = newAge;
        };
    
        this.equals = function(otherCat) {
            if (this.fields['age'] === otherCat.getAge() && 
                this.fields['name'] === otherCat.getName())  {
                return true;
            } else {
                return false;
            }
        };
    };
    
    module.exports = function(newFields) {
        var instance = new Cat();
        instance.fill(newFields);
        return instance;
    };
    
  • thefourtheye
    thefourtheye about 10 years
    @Chingy I am assuming yours. Try with == instead of === :)
  • Asqan
    Asqan about 5 years
    or now better in ES6.
  • Asqan
    Asqan about 5 years
    I came to give the same answer and saw this -4 answer, omg. What's wrong with it?
  • Amir Asyraf
    Amir Asyraf almost 3 years
    @Asqan maybe because the question asks for JS, not TypeScript. And you can't 'use' TypeScript in the browser, it needs to be compiled to JS first.
  • basarat
    basarat almost 3 years
    I've removed TypeScript from the answer as really modern JavaScript has the feature (classes) I'm recommending.
  • Miftakhul Arzak
    Miftakhul Arzak about 2 years
    How to use that class on another js file? I've tried import { Cat } from "./app/models/cat.model.js" but I got error Cannot use import statement outside a module