Monday, 15 August 2011

Rewrite javascript function into node.js module -



Rewrite javascript function into node.js module -

i have function: (which guess abstract mill 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(); } (var key in definition) { constructor.prototype[key] = definition[key]; } constructor.prototype.constructor = constructor; homecoming constructor; };

a utilize defining classes c/java syntax polymorphism , 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 calling:

var bullet = new bullet(params);

i tried rewrite nodejs module 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(); } (var key in definition) { constructor.prototype[key] = definition[key]; } constructor.prototype.constructor = constructor; homecoming constructor; }; module.exports.createclass = function() { homecoming $class(); } });

and phone call with:

var c = require(__dirname + "\\class"); var bullet = c.createclass({ extends: gameobject, constructor: function() {} });

but doesn't work, can please help me rewriting?

update: rewrited @salem answer, lost extending , polymorphism in process. in order have extending have write instead of

extends: parentclass

this:

extends: parentclass.constructor

i've expected polymorphism like:

// in class extended parentclass parentclass.method(); // in parent class adding line module.exports.method = parentclass.method;

but undefined. catch?

finally used mochiscript module nodejs, improve syntax sugar more object oriented functionality.

in code, createclass function without parameter. phone call $class without paramter also.

you don't need wrap code in function, because declare there won't accessible outside unless export it. should 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(); } (var key in definition) { constructor.prototype[key] = definition[key]; } constructor.prototype.constructor = constructor; homecoming constructor; }; module.exports.createclass = func;

this means if require module x, thing can access x.createclass, , not x.func or else.

javascript node.js rewriting

No comments:

Post a Comment