[英]Rewrite javascript function into node.js module
I have this function: (which is I guess abstract factory for creating javascript objects) 我有这个功能:(我猜这是用于创建javascript对象的抽象工厂)
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: 使用它来定义类C / java语法,例如多态性和扩展:
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: 我试图将其重写为nodejs模块,如下所示:
(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. 更新 :我从@Salem答案重写了它,但是我在过程中失去了扩展性和多态性。 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. 最后,我用mochiscript模块的NodeJS,那就更好了语法糖有更多的面向对象的功能。
In your code, createClass
is a function without any parameter. 在您的代码中,
createClass
是一个没有任何参数的函数。 Also you call $class
without any paramter also. 另外,您也调用
$class
而没有任何参数。
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. 这意味着,如果您将此模块要求为
X
,则唯一可以访问的是X.createClass
,而不是X.func
或其他任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.