[英]JS: Refactoring Code into OOP
I coded an application that isn't entirely OOP. 我编写了一个不完全面向对象的应用程序。 I was considering converting it to "real" OOP.
我正在考虑将其转换为“真实”的OOP。
Right now, the code is structured by setting subfunctions as attributes to main functions. 现在,通过将子功能设置为主要功能的属性来构造代码。
Ex: Bullet.move.normal
is a subfunction/dependency or Bullet.move
. 例如:
Bullet.move.normal
是子功能/依赖项或Bullet.move
。
This is how the code looks right now: 现在的代码如下所示:
Bullet = function(x,y,angle,type){
return {x:x,y:y,angle:angle,type:type};
}
Bullet.update = function(b){
Bullet.move(b);
Bullet.collision(b);
}
Bullet.move = function(b){
if(b.type === 'normal') Bullet.move.normal(b);
else if(b.type === 'sinus') Bullet.move.sinus(b);
}
Bullet.move.normal = function(b){
b.x += Math.cos(b.angle); b.y += Math.sin(b.angle); //not important
}
Bullet.move.sinus = function(b){
b.x += Math.cos(b.x); b.y += Math.sin(b.y); //not important
}
Bullet.collision = function(b){
Bullet.collision.wall(b);
Bullet.collision.actor(b);
}
Bullet.collision.wall = function(b){}
Bullet.collision.actor = function(b){}
---
I've started to write the OOP version of the code above but the structure I had doesn't work perfectly. 我已经开始编写上面的代码的OOP版本,但是我的结构无法正常工作。
this
parameter doesn't refer to the object if it's a "multilevel" function. 如果它是“多级”功能,则
this
参数不引用该对象。 (Check Bullet.prototype.move.normal
) (检查
Bullet.prototype.move.normal
)
What would be the recommended way to restructured the prototype without having to put all subfunctions in the main function? 在无需将所有子功能都放在主功能中的情况下,重构原型的推荐方法是什么? (Check 2nd
Bullet.prototype.move
) (检查2nd
Bullet.prototype.move
)
Is there a solution other than just naming everything like Bullet.prototype.move_normal
? 除了仅命名
Bullet.prototype.move_normal
东西之外,还有其他解决方案吗? I'd prefer to not have everything on the same "level". 我希望不要将所有内容都放在同一“级别”上。
And what would be the advantages of using OOP instead of what I had before? 与以前相比,使用OOP有什么好处? Is it worth converting to OOP?
值得转换为OOP吗?
Bullet = function(x,y,angle,type){
this.x = x;
this.y = y;
this.angle = angle;
this.type = type;
}
Bullet.prototype.update = function(){
this.move();
this.collision();
}
Bullet.prototype.move = function(){
if(this.type === 'normal') this.move.normal();
else if(this.type === 'sinus') this.move.sinus();
}
Bullet.prototype.move.normal = function(){
//not working, this === Bullet.prototype.move, not the bullet
this.x += Math.cos(this.angle); //not important
this.y += Math.sin(this.angle);
}
Bullet.prototype.move = function(){ //I dont want this. I'd like to keep things separated.
if(this.type === 'normal'){
this.x += Math.cos(this.angle);
this.y += Math.sin(this.angle);
}
else if(this.type === 'sinus'){
this.x += Math.cos(this.x);
this.y += Math.sin(this.y);
}
}
Replace type code with subclasses would be a good starting point: 用子类替换类型代码将是一个很好的起点:
function extend(Parent, Child) {
function Dummy () {}
Dummy.prototype = Parent.prototype;
Child.prototype = new Dummy();
Child.prototype.constructor = Parent;
}
Bullet = function(x, y, angle, type){
this.x = x;
this.y = y;
this.angle = angle;
this.type = type;
};
Bullet.prototype.update = function(){
this.move();
this.collision();
};
Bullet.prototype.collision = function(b){
this.collisionWall(b);
this.collisionActor(b);
};
Bullet.prototype.collisionWall = function(b){};
Bullet.prototype.collisionActor = function(b){};
//NormalBullet
NormalBullet = function() {
//Call parent constructor and pass all the arguments in.
Bullet.apply(this, arguments);
};
//Set prototype inheritance.
extend(Bullet, NormalBullet);
//Move the bullet move logic into subclass.
NormalBullet.prototype.move = function() {
this.x += Math.cos(this.angle);
this.y += Math.sin(this.angle);
};
//SinusBullet
SinusBullet = function() {
Bullet.apply(this, arguments);
};
extend(Bullet, SinusBullet);
SinusBullet.prototype.move = function() {
this.x += Math.cos(this.x);
this.y += Math.sin(this.y);
};
var sinusBullet = new SinusBullet(//your arguments);
sinusBullet.move();
var normalBullet = new NormalBullet(//your arguments);
normalBullet.move();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.