Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism nodeJS

I develop a restful API with nodeJS.

    exports.postCreature = function (req, res) {

    var creature = new Creature({
            name: req.body.name, id_user: req.user._id
        });

    creature.save(function (err) {
        if (err)
            res.status(400).send(Error.setError('impossible to save the your creature', err));
        else
            res.status(201).send();
    });
};

//CODE DUPLICATE 
exports.createCreature = function(user, callback) {
    console.log('Creature created');
    var creature = new Creature({
        name: user.username, id_user: user._id
    });

    creature.save(function (err) {
        if (err)
           callback(err, null);
        else
            callback(null, creature);
    });
}

The two functions execute the same code but not with the same parameters. I would like to avoid duplication in my code.

How can do in order to avoid duplication of my code ?

like image 521
Bob Avatar asked Apr 15 '15 19:04

Bob


People also ask

What is polymorphism in node JS?

The polymorphism is a core concept of an object-oriented paradigm that provides a way to perform a single action in different forms. It provides an ability to call the same method on different JavaScript objects. As JavaScript is not a type-safe language, we can pass any type of data members with the methods.

Is polymorphism possible in JS?

Polymorphism with Functions and Objects: It is also possible in JavaScript that we can make functions and objects with polymorphism.

How polymorphism is achieved in JS?

Polymorphism takes advantage of inheritance in order to make this happen. In the following example child objects such as 'cricket' and 'tennis' have overridden the 'select' method called from parent object 'game' and returned a new string respectively as shown in the output.

What is difference between polymorphism and inheritance?

Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class). Whereas polymorphism is that which can be defined in multiple forms. 2. It is basically applied to classes.


1 Answers

I would create another function to handle the redundancies:

function createCreature (creatureName, user, callback) {
    console.log('Creature created');
    var creature = new Creature({
        name: creatureName, id_user: user._id
    });

    creature.save(function (err, creature) {
        if (err)
           callback(err, null);
        else
            callback(null, creature);
    });
}

And then in your other functions:

exports.postCreature = function (req, res) {
    createCreature(req.body.name, req.user, function (err, creature) {
        if (err)
            res.status(400).send(Error.setError('impossible to save the your creature', err));
        else
            res.status(201).send();
    };
};

exports.createCreature = function(user, callback) {
    console.log('Creature created');
    createCreature (user.username, user, callback);
}
like image 167
KJ Price Avatar answered Oct 21 '22 05:10

KJ Price