Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a object oriented Node.js model

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;
};
like image 680
Wang-Zhao-Liu Q Avatar asked Mar 25 '14 04:03

Wang-Zhao-Liu Q


People also ask

Is OOP used in Nodejs?

Tl;dr Absolutely not. I would personally say that writing Node. js in OOP style is just going to slow you down.

What is oops concept in node JS?

Over time the technologies or tools that we use in programming come and go but Object Oriented Programming which is abbreviated by OOP remains stable over the past decade. Because it is not a programming language or tool but it is a paradigm or style of programming.

Can we use OOP in JavaScript?

JavaScript is not a class-based object-oriented language. But it still has ways of using object oriented programming (OOP).


1 Answers

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
like image 93
thefourtheye Avatar answered Sep 17 '22 14:09

thefourtheye