JS:将代码重构为OOP

时间:2014-06-17 03:53:29

标签: javascript oop object refactoring structure

我编写了一个不完全是OOP的应用程序。我正在考虑将其转换为“真正的”OOP。

现在,通过将子功能设置为主要功能的属性来构建代码。

例如:Bullet.move.normal是子功能/依赖项或Bullet.move

这就是代码现在的样子:

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){}


---

我已经开始编写上面代码的OOP版本,但我的结构并不完美。

如果

this参数是“多级”函数,则它不引用该对象。 (检查Bullet.prototype.move.normal

在不必将所有子功能放入主函数的情况下,重构原型的推荐方法是什么? (检查第二个Bullet.prototype.move

除了命名Bullet.prototype.move_normal之类的所有内容之外,还有其他解决方案吗?我宁愿不把所有东西放在同一“水平”上。

使用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);    
    }
}

1 个答案:

答案 0 :(得分:1)

用子类替换类型代码将是一个很好的起点:

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();

Source