Search code examples
javascriptnode.jsrewriting

Rewrite javascript function into node.js module


I have this function: (which is I guess abstract factory for creating javascript objects)

var $class = function(definition) {
    var constructor = definition.constructor; 
    var parent = definition.Extends;
    if (parent) {
        var F = function() { };
        constructor._superClass = F.prototype = parent.prototype;
        constructor.prototype = new F();
    }
    for (var key in definition) {
        constructor.prototype[key] = definition[key];
    }
    constructor.prototype.constructor = constructor;
    return constructor;
};

A use it for defining classes C/java syntax like with polymorphism and extending:

var Bullet = $class({

    Extends: GameObject,

    constructor: function(texturePath, x, y, ctx, direction, passable, player) {
        GameObject.call(this, texturePath, x, y, ctx, 1, 1, passable, new Array(8, 8, 0, 0));
    },

    isActive: function() {
    },

    getPlayer: function() {
    },

    update: function(dt) {
    },

    destroy: function() {
    },

    processCollision: function() {
    }
});

And then calling:

var bullet = new Bullet(params);

I tried to rewrite it into nodejs module like this:

(function(){
var $class = function(definition) {
    var constructor = definition.constructor; 
    var parent = definition.Extends;
    if (parent) {
        var F = function() { };
        constructor._superClass = F.prototype = parent.prototype;
        constructor.prototype = new F();
    }
    for (var key in definition) {
        constructor.prototype[key] = definition[key];
    }
    constructor.prototype.constructor = constructor;
    return constructor;
};
module.exports.createClass = function() {
    return $class();
}
});

And then call it with:

var c = require(__dirname + "\\Class");
var Bullet = c.createClass({
   Extends: GameObject,
    constructor: function() {}
});

But it doesn't work, can you please help me with rewriting?

UPDATE: I rewrited it from @Salem answer, but I lost extending and polymorphism in process. In order to have extending I simply have to write instead of

Extends: ParentClass

this:

Extends: ParentClass.constructor

I've expected that polymorphism would be something like:

// in class which is extended from ParentClass
ParentClass.method();

// in parent class adding line
module.exports.method = ParentClass.method;

But this is undefined. So where is the catch?

FINALLY I used mochiscript module for nodejs, it is even better syntax sugar with more object oriented functionality.


Solution

  • In your code, createClass is a function without any parameter. Also you call $class without any paramter also.

    You don't need to wrap all your code in a function, because everything you declare there won't be accessible from outside unless you export it. So it should be something like this:

    var func = function(definition) {
        var constructor = definition.constructor; 
        var parent = definition.Extends;
        if (parent) {
            var F = function() { };
            constructor._superClass = F.prototype = parent.prototype;
            constructor.prototype = new F();
        }
        for (var key in definition) {
            constructor.prototype[key] = definition[key];
        }
        constructor.prototype.constructor = constructor;
        return constructor;
    };
    
    module.exports.createClass = func;
    

    This means that if you require this module as X, the only thing you can access is X.createClass, and not X.func or anything else.